I want to display different messages to users based on the API error.
the problem is that Dio intercepts all 409
errors as a conflict and displays the same message every time.
I/flutter ( 4507): 🐛 12:06:44.286125 DEBUG Global Loggy - DioError [bad response]: The request returned an invalid status code of 409.
but API error response is:
{
"title": "PALLET_NOT_CREATED",
"status": 409,
"detail": "Cannot create a new pallet, the preparer has already a pallet in status IN_PROGRESS",
"timestamp": 1686218442060,
"developerMessage": "SOME MESSAGE ERE",
"code": "WMS_CLT_ERR_4",
"errors": {}
}
is there's anyway i can use or handle errors without dioErrors ?
my DioService:
...
Future<Response<JSON>> post({
required String endpoint,
JSON? data,
Options? options,
CancelToken? cancelToken,
}) async {
final response = await _dio.post<JSON>(
baseUrl + endpoint,
data: data,
options: options,
cancelToken: cancelToken ?? _cancelToken,
);
return response;
}
...
NOTE: THIS IS ONLY THE POST REQUEST I HANDLED ALL OTHER METHODS`
service calling API
Future<PalletModel> createPallet(int batchId) async {
try {
final res = await _dioService.post(
endpoint: CommonProperties.palletsByBatchId.replaceAll(
":batchId",
batchId.toString(),
),
data: {},
options: Options(
extra: {
'requiresAuthToken': true,
},
),
);
return PalletModel.fromJson(res.data!);
} catch (e) {
logDebug('error is:');
logDebug(e);
rethrow;
}
}
you can apply try/cath
to your api post
call
Future<Response<JSON>> post({
required String endpoint,
JSON? data,
Options? options,
CancelToken? cancelToken,
}) async {
try {
final response = await _dio.post<JSON>(
baseUrl + endpoint,
data: data,
options: options,
cancelToken: cancelToken ?? _cancelToken,
return response;
);
}
on DioError catch(e) {
print(e.response!.data.toString)
}
}
After catching a DioError, you can manipulate data, checking response body, etc