I have been trying to implement the exceptions while calling Rest APIs, I have done a basic implementation of exceptions in a try-catch block. But I need to throw error dialogs on different status codes i.e: either 500, 503, 403
I am attaching my code snippets in order to better understand what exactly I am trying to implement.
try {
var response = await client
.post(
Uri.https(
"api base url", "api end point"),
body: param)
.timeout(Duration(seconds: TIME_CONST))
.catchError(handleError);
if (response.statusCode == 200) {
print('Response Body: ${response.body}');
final data = jsonDecode(response.body);
return UserCheck(
user: data['user_exists'],
);
} else {
print("The response status is not 200");
return param;
}
} on SocketException {
throw FetchDataException('message', 'url');
} on TimeoutException {
throw ApiNotRespondingException("message", "url");
}
Any help will be appreciated. Have been stuck on this problem for quite a few days.
Aucun commentaire:
Enregistrer un commentaire