dio icon indicating copy to clipboard operation
dio copied to clipboard

I can't return my own errors class in onError interceptor

Open andrefedev opened this issue 2 months ago • 0 comments

Request Statement

So, I don't want to have to extend DioException. I want to be able to use interceptors to handle DioException errors with my own error class that extends Exception without dying trying. But it definitely doesn't work. There is no information about this in the documentation. I have a package that is an api and I don't want the applications that use it to have to install Dio just to handle DioException or in the worst case if I changed to Dio for whatever we would have to make a big change.

Solution Brainstorm

// ##########################
// # ErrorHandle Middleware #
// ##########################

interceptors.add(
  InterceptorsWrapper(
    onError: (DioException error, ErrorInterceptorHandler handler) {
      switch (error.type) {
        case DioExceptionType.badResponse:
          // throw ApiException.fromBadResponse(response);
          return handler.reject(_handleBadResponse(error));
        case DioExceptionType.connectionError:
          // // throw ApiException.fromBadResponse(response);
          return handler.reject(_handleConnectionError):

        default:
          return handler.reject(error);
      }
    },
  ),
);

andrefedev avatar May 05 '24 14:05 andrefedev