onError method

  1. @override
void onError(
  1. DioError err,
  2. ErrorInterceptorHandler handler
)

The callback will be executed on error.

If you want to continue the error , call handler.next.

If you want to complete the response with some custom data directly, you can resolve a Response object with handler.resolve and other error interceptor(s) will be skipped.

If you want to complete the response with an error message directly, you can reject a DioError object with handler.reject, and other error interceptor(s) will be skipped.

Implementation

@override
void onError(DioError err, ErrorInterceptorHandler handler) {
  debugPrint("在错误之前进行拦截");

  switch (err.type) {
    case DioErrorType.connectTimeout:
      debugPrint("请求超时");
      break;
    case DioErrorType.sendTimeout:
      debugPrint("发送超时");
      break;
    case DioErrorType.receiveTimeout:
      debugPrint("响应超时");
      break;
    case DioErrorType.response:
      debugPrint("服务器繁忙");
      break;

    case DioErrorType.other:
      debugPrint("其他错误");
      break;
    case DioErrorType.cancel:
      debugPrint("取消请求");
      break;
  }
  super.onError(err, handler);
}