Dio: How to override Error thrown in OnError Interceptor?

Created on 2 Oct 2018  路  4Comments  路  Source: flutterchina/dio

Hello,
thanks for developing this library, it's very useful.
I have a question, I want to throw my own custom exception in OnError interceptor. However it's not working because in the catch block the error caught is DioError not my own exception. Can I override that to throw my exception?
Example of my interceptor

if (error.type == DioErrorType.RECEIVE_TIMEOUT ||
          error.type == DioErrorType.CONNECT_TIMEOUT) {
        throw TimeoutException();
      } else if (error.type == DioErrorType.RESPONSE) {
        switch (error.response.statusCode) {
          case 401:
            throw UnauthorizedException();
          case 403:
            throw ForbiddenException();
          case 404:
            throw NotFoundException();
          case 500:
            throw InternalServerError(message: error.message);
        }
      } else {
        return error;
      }

Thank you.

Most helpful comment

I have found a solution. I can do this by extending DioError.

All 4 comments

I have found a solution. I can do this by extending DioError.

Hi @skybur Would you mind to share how did you solve this problem?

Cheers!

Just extends DioError on your custom exception
example:

class MyCustomException extends DioError
{
// code here
}

Thank you, @skybur I have just done the same. Thank you cheers!

Was this page helpful?
0 / 5 - 0 ratings