Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use handler.reject in onError of QueuedInterceptor #2092

Closed
jackie-weiwei opened this issue Jan 10, 2024 · 1 comment
Closed

How to use handler.reject in onError of QueuedInterceptor #2092

jackie-weiwei opened this issue Jan 10, 2024 · 1 comment

Comments

@jackie-weiwei
Copy link

jackie-weiwei commented Jan 10, 2024

Request Statement

flutter version: 3.10.4
dio version : 5.4.0

When I call handler.reject(DioException(requestOptions: options)) in onError, The caller displays the following error:

Exception has occurred.
DioException (DioException [unknown]: null)

image

I don't understand now why the caller reports an error after rejecting. How should I use reject? Please help. Thank you.
my code like this:

Response res = await RestApi.post(
    path: "/test/test", data: {"mobile": "1000000000"});
if (res == null) {
  print("请求失败");
}

interceptor code like this:

    _dio.interceptors.add(QueuedInterceptorsWrapper(
      onRequest: (options, handler) async {
        options.headers["access"] = _token;
        return handler.next(options); //continue
      },
      onResponse: (response, handler) {
        String path = response.requestOptions.path;
        if (path.contains("login")) {
          _token = response.data["accessToken"];
          _token2 = response.data["refreshToken"];
        }
        debugPrint(path);
        return handler.next(response); // continue
      },
      onError: (error, handler) async {
        if (error.response?.statusCode == 401) {
          final options = error.response!.requestOptions;
          bool bSucc = await refreshToken();

          if (bSucc) {
            var res = await _dio.fetch(error.response!.requestOptions);
            handler.resolve(res); //continue
          } else {
            handler.reject(DioException(requestOptions: options));
          }
        } else {
          handler.next(error);
        }
      },
    ));

Solution Brainstorm

No response

@jackie-weiwei jackie-weiwei added the s: feature This issue indicates a feature request label Jan 10, 2024
@AlexV525
Copy link
Member

Please use discussions for general issues.

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
@AlexV525 AlexV525 added i: not related and removed s: feature This issue indicates a feature request labels Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants