解决 Flutter Dio合并请求多个接口,如果一个接口500,那么导致其他请求不在执行

发布于:2024-12-06 ⋅ 阅读:(105) ⋅ 点赞:(0)

Flutter Dio如何自定义拦截异常

应用场景

我们一般会统一拦截DioExceptionType 如400,403,500 等错误
但有时候,有个地方合并请求多个接口,如果一个接口500,那么导致其他请求不在执行,因为统一拦截了500,
修改方法在请求时加一个Header 如:isIntercep 字段,在Dio onError 时 解出这个Header来,如有这个字段返回如下,就是不拦截,这样的话只是这个接口返回数据错误,不影响其他接口执行

 handler.resolve(
        Response<Map<String, dynamic>>(
          requestOptions: err.requestOptions,
          statusCode: 200,
          data: {},
        ),

///请求公共参数拦截器
class HttpParamsInterceptor extends Interceptor {
  static const language = "language";
  static const appname = "appname";
  static const mobileType = "mobileType";
  static const mobileId = "mobileId";
  static const version = "version";

  
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    var headers = options.headers;
    headers[language] = LocaleController.getCountryCode;
    headers[mobileType] = GetPlatform.isAndroid ? "Android" : "IOS";
    headers[version] = PackageInfoManager().version;
    headers[appname] = PackageInfoManager().appName;
    if (AppConfig.instance.enableRequestJsonLog) {
      Log.r(
          '${"--------------------onRequest---------------"}\n${options.method}${' Url:'} ${options.uri}\n${'Query Parameters:'}${options.queryParameters}\n${'Body:'}${options.data != null ? const JsonEncoder.withIndent('  ').convert(options.data) : ""}\n\n');
    }
    super.onRequest(options, handler);
  }

  
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (AppConfig.instance.enableResponseJsonLog) {
      Log.r(
          '${"-----------------------------Response Start-----------------------------"}\n${response.requestOptions.method}${' Url:'} ${response.requestOptions.uri}\n${'Query Parameters:'}${response.requestOptions.queryParameters}\n${'Body:'}${response.requestOptions.data != null ? const JsonEncoder.withIndent('  ').convert(response.requestOptions.data) : ""}\n${'response data:'}\n$response\n\n${"-----------------------------Response End-------------------------------"}\n\n\n');
    }
    super.onResponse(response, handler);
  }

  
  Future<void> onError(
      DioException err, ErrorInterceptorHandler handler) async {
    AppException appException = AppException.create(err);
    if (AppConfig.instance.enableRequestLog) {
      Log.e('DioException===: +${err.toString()}');
    }
    Log.logPrint(err.requestOptions.headers);
    Log.logPrint(appException.type);
    Map<String, dynamic> data = err.requestOptions.headers;
    bool? isIntercep = data[APIConstant.ignoreIntercep];
    if (isIntercep == true &&
        (appException.type == DioExceptionType.badResponse ||
            appException.type == DioExceptionType.unknown)) {
      return handler.resolve(
        Response<Map<String, dynamic>>(
          requestOptions: err.requestOptions,
          statusCode: 200,
          data: {},
        ),
      );
    }

    return handler.next(appException);
  }
}


网站公告

今日签到

点亮在社区的每一天
去签到