Dio won't catch errors and get stuck on throwing custom exception

Viewed 246

Here is my code for the onError for interceptors. I am trying to throw a custom exception by using custom exception classes

 @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);
  }

I am unable to catch up on this section pointer stuck on throw custom exception

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----------------");

    }
  }
2 Answers

first of all your custom exception classes should be extended from DioError class.

for example this is one of custom exceptions that I've created:

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

then instead of throwing exceptions in onError method, call super.onError(CustomException, handler);

this is what I did:

  @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);
    }
  }

Since you have specified on UnauthorizedException it will only catch that type of exception. you need to remove on UnauthorizedException to catch all exception

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----------------");

}

}

Related