-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
A request with an interceptor, which produces an error, makes the code stuck. #2138
Comments
It seems I have broken the interface contract. The |
Resolves #2138 ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [ ] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package --------- Signed-off-by: CaiJingLong <cjl_spy@163.com> Co-authored-by: Alex Li <github@alexv525.com>
Reverts #2139 Fixes #2167 Reopen #2138 ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [ ] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) Added tests were fake, so they remained effective after the revert.
the same problem is happening with our project too. |
We're currently experiencing the same issue using the following interactor to refresh an access token and retry the request, using Dio v5.4.3+1. The request never finishes executing and no exceptions are caught in the try/catch block. try {
final body = {...};
final options = Options(extra: {'authenticated': true}, validateStatus: (status) => status == 200);
final response = await _dio.post('...', data: body, options: options);
// We never get here
} on DioException catch (error, trace) {
// We never get here
} class AuthInterceptor extends Interceptor {
final Dio _dio;
final Future<String?> Function() _getAccessToken;
AuthInterceptor({
required Dio dio,
required Future<String?> Function() getAccessToken,
}) : _dio = dio,
_getAccessToken = getAccessToken;
@override
Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
// If the request is not authenticated, continue without adding the Authorization header
if (options.extra['authenticated'] == false) {
return handler.next(options);
}
// Try to retrieve a valid (non-expired) access token for the Authorization header.
// If no token could be retrieved, we don't add the header.
final String? accessToken = await _getAccessToken();
if (accessToken != null) {
options.headers['Authorization'] = 'Bearer $accessToken';
}
// Continue request
return handler.next(options);
}
@override
FutureOr<void> onError(DioException error, ErrorInterceptorHandler handler) async {
final Response? response = error.response;
// We're only interested in handling the 401 header here. Let other errors pass through.
if (response == null || response.statusCode != 401) {
return handler.next(error);
}
final RequestOptions options = response.requestOptions;
// If the request was already retried we back off and let the error pass through.
if (options.extra['retried'] == true) {
return handler.next(error); // also tried handler.reject(error) with same result
}
// Add flag to indicate that we retried the request, to prevent infinite retries.
options.extra['retried'] = true;
// Retry the request. A refresh token will be added to the request in the [onRequest]
// handler, when available.
return handler.resolve(await _dio.fetch(options));
}
} |
Package
dio
Version
5.4.1
Operating-System
Android
Output of
flutter doctor -v
Dart Version
3.3.1
Steps to Reproduce
Here is a minimal example; I can't understand why the error isn't caught in any way, and everything that follows the request never executes.
tried on Android and macOS
Expected Result
Errors from broken requests must be caught.
Actual Result
The program stops when executing a request, and the subsequent code becomes unreachable.
The text was updated successfully, but these errors were encountered: