Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i am unable to catch dio error #1198

Closed
mohadel92 opened this issue Jun 19, 2021 · 8 comments
Closed

i am unable to catch dio error #1198

mohadel92 opened this issue Jun 19, 2021 · 8 comments

Comments

@mohadel92
Copy link

New Issue Checklist

  • [true ] I have searched for a similar issue in the project and found none

Issue Info

Info Value
Platform Name e.g. flutter / ios / android
Platform Version e.g. 2.2.0 / 12.0 / 9.0
Dio Version e.g. 4..0.0
Android Studio / Xcode Version e.g. Android Studio 4.1 / Xcode 12.4

Issue Description and Steps

i used to use dio since older version
i catch errors with

 e.response.data
e.response.headers

now after migrating to null safety
and flutter 2.2 i cannot catch any errors i get message

any help?
Whats-App-Image-2021-06-19-at-10-58-44-PM

@bagus-repository
Copy link

please make sure you are catching a DioError Object see Handling Error.

@dhsont
Copy link

dhsont commented Jun 22, 2021

DioError is not capturing any 400-> 500 errors

@mohadel92
Copy link
Author

@bagus-repository thanks but i have never used this method
and all apps used to work fine

@rymesaint
Copy link

DioError is not capturing any errors, because when i'm adding headers to the options

@ApoorvaAditya
Copy link

I had a similar problem when I migrated my app to null safety and dio from 3.0.10 to 4.0.0. After bunch of testing I found out that an onError interceptor was the cause of the problem for me. The interceptor basically consumed the error and didn't pass the error back to the calling function and therefore no exception/error was being caught.

I fixed it by using the ErrorInterceptorHandler parameter of the onError function of the interceptor and adding a ErrorInterceptorHandler.reject(error); at the end of the onError function. Here's an example:

dio.interceptors.add(
  InterceptorsWrapper(
    onError: (error, handler) {
      // Do stuff here
      handler.reject(error); // Added this line to let error propagate outside the interceptor
    },
  ),
);

@sopherwang
Copy link

I had a similar problem when I migrated my app to null safety and dio from 3.0.10 to 4.0.0. After bunch of testing I found out that an onError interceptor was the cause of the problem for me. The interceptor basically consumed the error and didn't pass the error back to the calling function and therefore no exception/error was being caught.

I fixed it by using the ErrorInterceptorHandler parameter of the onError function of the interceptor and adding a ErrorInterceptorHandler.reject(error); at the end of the onError function. Here's an example:

dio.interceptors.add(
  InterceptorsWrapper(
    onError: (error, handler) {
      // Do stuff here
      handler.reject(error); // Added this line to let error propagate outside the interceptor
    },
  ),
);

Hi @ApoorvaAditya Thanks for the solution. But I's still not able to catch the exceptions. I can see the exceptions go through the intercepted callbacks but after reject the error, it exception still unhandled. Would you mind providing more detailed solution? Thanks.

@stale
Copy link

stale bot commented Aug 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Aug 21, 2021
@stale stale bot closed this as completed Sep 3, 2021
@shakthizen
Copy link

I found a hacky solution. We are overriding all the exceptions thrown by default. Then we are going to create custom interceptor to handle and throw exceptions. This way we can catch the DioException as we did before.

  1. Create a base client like this. You have to set validateStatus function to return true whatever the status code is.
final baseOptions = BaseOptions(
  baseUrl: host,
  contentType: Headers.jsonContentType,
  validateStatus: (int? status) {
    return status != null;
    // return status != null && status >= 200 && status < 300;
  },
);

final dio = Dio(baseOptions);
  1. Create a custom interceptor to raise exceptions
class ErrorInterceptor extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    final status = response.statusCode;
    final isValid = status != null && status >= 200 && status < 300;
    if (!isValid) {
      throw DioException.badResponse(
        statusCode: status!,
        requestOptions: response.requestOptions,
        response: response,
      );
    }
    super.onResponse(response, handler);
  }
}
  1. Add that to the Dio interceptors list
dio.interceptors.addAll([
    ErrorInterceptor(),
]);

Link to my gist https://gist.github.com/shakthizen/d644aaf19ca837a4a29a92ebad551055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants