Error Handling¶
When an API call results in an error, it is first converted into a native (Android or iOS) exception. All native exceptions are handled by Flutter and are represented by the PlatformException
class. PlatformException can caught using a catchError
callback as shown below:
Backendless.data.of("NonExistentTable").findFirst()
.then((response) {
print("Response: $response");
})
.catchError((error) {
print("Server reported an error: $error");
});
The PlatformException object delivered into the catchError callback has two properties:
code
- is a numeric value containing the code of the error. See the complete listing of Backendless error codes.details
- is a text description of the error.
The example below demonstrates how your code can access these properties:
Backendless.data.of("NonExistentTable").findFirst()
.then((response) {
print("Response: $response");
})
.catchError((error) {
PlatformException platformException = error;
print("Server reported an error.");
print("Exception code: ${platformException.code}");
print("Exception details: ${platformException.details}");
}, test: (e) => e is PlatformException);
The code produces the following output:
Server reported an error.
Exception code: 1009
Exception message: Table not found by name. Table not found by name 'NonExistentTable'.
Make sure the client class referenced in the API call has the same literal name as the
table in Backendless console'
Additionally, it is possible to obtain the underlying stacktrace by adding an argument to the catchBlock
as shown below:
Backendless.data.of("NonExistentTable").findFirst()
.then((response) {
print("Response: $response");
})
.catchError((error, stackTrace) {
PlatformException platformException = error;
print("Server reported an error.");
print("Exception code: ${platformException.code}");
print("Exception details: ${platformException.details}");
print("Stacktrace: $stackTrace");
}, test: (e) => e is PlatformException);
Error Codes¶
Click here for the complete list of Backendless Error Codes