【问题标题】:Dio won't catch errors and get stuck on throwing custom exceptionDio 不会捕获错误并陷入抛出自定义异常
【发布时间】:2022-08-08 20:21:35
【问题描述】:

这是我的拦截器 onError 代码。我正在尝试通过使用自定义异常类来引发自定义异常

 @override
  Future<void> onError(DioError err, ErrorInterceptorHandler handler) async {

    switch (err.type) {
      case DioErrorType.connectTimeout:
      case DioErrorType.sendTimeout:
      case DioErrorType.receiveTimeout:
        throw DeadlineExceededException(err.requestOptions);
      case DioErrorType.response:
        switch (err.response?.statusCode) {
          case 400:
            throw BadRequestException(err.requestOptions);
          case 401:
            throw UnauthorizedException(err.requestOptions);

          case 404:
            throw NotFoundException(err.requestOptions);
          case 409:
            throw ConflictException(err.requestOptions);
          case 500:
            throw InternalServerErrorException(err.requestOptions);
        }
        break;
      case DioErrorType.cancel:
        break;
      case DioErrorType.other:
        throw NoInternetConnectionException(err.requestOptions);
    }
   // super.onError(err, handler);
     return handler.next(err);
  }

我无法赶上这部分指针卡在抛出自定义异常上

static requestMyJobs() async {
    try {
      print(\'---------job calling api---------\');
      var response = await ApiBase.dio.get(ApiLinks.getMyJobsLink);
      print(\'Status code ${response.statusCode}\');
      var jocodedData = response.data[\'data\'];
      return jocodedData.map<MyJobs>((json) => MyJobs.fromJson(json)).toList();
    } on UnauthorizedException catch (f) {
      print(\"-Exception----------------\");

    }
  }

    标签: flutter dart interceptor dio onerror


    【解决方案1】:

    首先,您的自定义异常类应该从 DioError 类扩展。

    例如,这是我创建的自定义异常之一:

    class ConnectionException extends DioError {
      ConnectionException({required super.requestOptions});
    }
    

    然后不要在 onError 方法中抛出异常,而是调用 super.onError(CustomException, handler);

    这就是我所做的:

      @override
      Future onError(
        DioError err,
        ErrorInterceptorHandler handler,
      ) async {
        switch (err.type) {
          case DioErrorType.connectTimeout:
          case DioErrorType.receiveTimeout:
          case DioErrorType.sendTimeout:
            throw TimeoutException();
          case DioErrorType.response:
            switch (err.response?.statusCode) {
              case 400:
                super.onError(BadRequestException(err.response?.data, requestOptions: err.requestOptions), handler);
                break;
              case 401:
                super.onError(UnAuthorizedException(requestOptions: err.requestOptions), handler);
                break;
              case 403:
                try {
                  await refreshToken();
                  await retryRequest(err.requestOptions);
                } catch (_) {
                  super.onError(UnAuthorizedException(requestOptions: err.requestOptions), handler);
                }
                break;
              case 500:
                super.onError(InternalServerException(requestOptions: err.requestOptions), handler);
                break;
            }
            break;
          case DioErrorType.other:
            super.onError(ConnectionException(requestOptions: err.requestOptions), handler);
            break;
          default:
            super.onError(err, handler);
        }
      }
    

    【讨论】:

      【解决方案2】:

      由于您指定了on UnauthorizedException,它只会捕获该类型的异常。您需要删除 on UnauthorizedException 以捕获所有异常

      static requestMyJobs() async {
      try {
        print('---------job calling api---------');
        var response = await ApiBase.dio.get(ApiLinks.getMyJobsLink);
        print('Status code ${response.statusCode}');
        var jocodedData = response.data['data'];
        return jocodedData.map<MyJobs>((json) => MyJobs.fromJson(json)).toList();
      } catch (f) {
        print("-Exception----------------");
      
      }
      

      }

      【讨论】:

      • 是的,我指定了所有类型,我在这里只提到了一种作为示例......
      • 这将捕获所有错误。
      • 没有。它不工作。指向案例 401 的运行时异常指针: throw UnauthorizedException(err.requestOptions);或其他发生并粘在其上的。它不会移动以捕捉块
      猜你喜欢
      • 2015-07-14
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多