Reputation: 800
How can I access the "Message" field inside the e
My postRequest function:
Future<Response> postRequest(String endPoint, dynamic data) async {
Response response;
try {
if (data != null) {
response = await _dio!.post(endPoint, data: data);
} else {
response = await _dio!.post(endPoint);
}
} catch (e) {
//I can reach the message from here and return it, but this
is not what I want to do.
if (e is DioError) print(e.response);
throw Exception(e);
}
return response;
}
where I want to reach the message:
var response;
try{
response = await http.postRequest("Stok/$type", requestObj);
}catch(e){
print("message....: $e");
}
Upvotes: 0
Views: 208
Reputation: 126
Future<Response> postRequest(String endPoint, dynamic data) async {
Response response;
try {
if (data != null) {
response = await _dio!.post(endPoint, data: data);
} else {
response = await _dio!.post(endPoint);
}
return response;
} on DioError {
rethrow; // or throw e;
}
}
Now, just call it like that
var response;
try{
response = await http.postRequest("Stok/$type", requestObj);
} on DioError catch(e){
print("message....: ${e.message}");
}
Upvotes: 1