GithubHelp home page GithubHelp logo

Comments (4)

Alleds avatar Alleds commented on May 23, 2024 1

@zubair999 try to add super.onError(err, handler)

from dio.

zubair999 avatar zubair999 commented on May 23, 2024

@zubair999 try to add super.onError(err, handler)

I already tried it but it 's not working. I try print in onError code block but request never there, and if reload the app then it print.

from dio.

AlexV525 avatar AlexV525 commented on May 23, 2024

Your reproducible code is not minimal, which means hard for us to reproduce the case easily. Please consider setting breakpoints and running the app with the debugger to see it it actually works.

from dio.

ykaito21 avatar ykaito21 commented on May 23, 2024

I have a similar problem. dio version 5.4.1

Expected Behavior:
After the request fails and triggers onError, the retried request within onError should also throw a DioException if it fails, which should then be caught by the try-catch block within the same onError method. This should allow for custom error handling or logging of the retried request failure.

Actual Behavior:
The retried request within onError triggers the onRequest interceptor again as expected, but if the retried request fails, the try-catch block within onError does not catch the subsequent DioException. This behavior prevents custom error handling for the retried request failure from being executed as intended.

Making a GET request...
onRequest is called
onError is called
onRequest is called
import 'package:dio/dio.dart';

class ErrorInterceptor extends QueuedInterceptor {
  final Dio dio;

  ErrorInterceptor(this.dio);

  @override
  Future<void> onError(
      DioException err, ErrorInterceptorHandler handler) async {
    print('onError is called');
    try {
      // This should throw an error
      await dio.fetch(err.requestOptions);
    } catch (e) {
      // Why cannot I catch the error here?
      print('onError is called again');
    }
    handler.next(err);
  }
}

class RequestInterceptor extends Interceptor {
  const RequestInterceptor();

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    print('onRequest is called');
    return handler.next(options);
  }
}

class ResponseIntercepter extends Interceptor {
  const ResponseIntercepter();

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    print('onResponse is called');
    handler.next(response);
  }
}

void main() async {
  var dio = Dio();

  // Add the custom interceptor
  dio.interceptors.addAll([
    const RequestInterceptor(),
    const ResponseIntercepter(),
    ErrorInterceptor(dio),
  ]);

  // Making a GET request
  try {
    print('Making a GET request...');
    Response response =
        await dio.get('https://example.com/this-does-not-exist');
    print(response.data);
  } catch (e) {
    print("Final error: $e");
  }
}

from dio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.