Home > Enterprise >  Can't catch error with dio package in flutter
Can't catch error with dio package in flutter

Time:10-15

I'm implementing the post method using dio package and when I try to catch exceptions in dioError it won't catch any error, I use interceptor and I found that the one rror method is never called by the app.

I searched in different articles and posts but nothing specific mentioned this issue, I used dio version 4.0.2 and still have the same problem , i tried to extend the DioError in my exception file and nothing happened

The post method :

@override
  Future post(
    String path, {
    Map<String, dynamic>? body,
    bool formDataIsEnabled = false,
    Map<String, dynamic>? queryParams,
  }) async {
    try {
      debugPrint("ENTERING THE POST METHOD:");
      final response = await client.post(path,
          queryParameters: queryParams,
          data: formDataIsEnabled ? FormData.fromMap(body!) : body);
      return _handleJsonResponse(response);
    } on DioError catch (error) {
        return _handleDioError(error);
    }
  }

The handling methods :

 dynamic _handleJsonResponse(Response<dynamic> response) {
    final jsonResponse = jsonDecode(response.data);
    return jsonResponse;
  }

  dynamic _handleDioError(DioError error) {
    switch (error.type) {
      case DioErrorType.connectTimeout:
        break;
      case DioErrorType.sendTimeout:
        break;
      case DioErrorType.receiveTimeout:
        throw FetchDataException();
      case DioErrorType.response:
        switch (error.response?.statusCode) {
          case StatusCode.badRequest:
            throw BadRequestException();
          case StatusCode.unauthorized:
          case StatusCode.forbidden:
            throw UnauthorisedException();
          case StatusCode.notFound:
            throw NotFoundException();
          case StatusCode.conflict:
            throw ConflictException();
          case StatusCode.internalServerError:
            throw InternalServerErrorException();
        }
        break;
      case DioErrorType.cancel:
        break;
      case DioErrorType.other:
        throw ServerExceptions(message: "Error");
    }
  }

The interceptor:

class AppInterceptors extends Interceptor {
  @override
  Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async{
    debugPrint('REQUEST [${options.method}]=>PATH: ${options.path}');
    options.headers[AppStrings.contentType] = AppStrings.applicationContentType;
    super.onRequest(options, handler);
  }

  @override
  Future onResponse(Response response, ResponseInterceptorHandler handler)async {
    debugPrint('RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path}');
     super.onResponse(response, handler);
  }
  @override
  Future one rror(DioError err, ErrorInterceptorHandler handler) async{
    debugPrint('ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path}');
   super.onError(err, handler);
  }

CodePudding user response:

Try to check the response status after the post request. Sometimes APIs do not generate exceptions so you need to add a check on the response statusCode.

  • Related