-
-
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
DioException on 401 before hitting an Authentication Interceptor #2056
Comments
|
@AlexV525 @override
Future<void> onError(
DioException err, ErrorInterceptorHandler handler) async {
// Check if the response status code is 401 (Unauthorized) and it's not the login request
if (err.response?.statusCode == 401
// && err.response?.requestOptions.path != authenticationLoginUrl
) {
// Retrieve the authentication token from local storage
final authenticationToken =
await LocalAuthenticationTokenIsarService.get();
// Extract the refresh token from the authentication token
final refreshToken = authenticationToken?.refreshToken;
// If a 401 response is received, refresh the access token
String? newAccessToken =
await _getAuthenticationToken(_dio, _apiBaseUrl, refreshToken ?? '');
if (newAccessToken != null) {
// Update the request header with the new access token
err.requestOptions.headers['Authorization'] = 'Bearer $newAccessToken';
// Repeat the request with the updated header
return handler.resolve(await _dio.fetch(err.requestOptions));
}
}
return handler.next(err);
} I still the request goes fine through the onRequest and gets shipped out with return handler.next(options) How and why i'm getting an DioException before, I realy can't figure out |
The same error, version 5.4.0 |
I'm currently AFK. Is it working in a previous version and break in the latest version? |
@AlexV525 But when I downgrade Flutter to version 3.13.9 Flutter 3.13.9: Flutter 3.16.x: |
Oh I overlook the issue. This is not related to any actual code. Duplicate of #1869 |
This comment was marked as off-topic.
This comment was marked as off-topic.
If I run my app WITH debug (VS Code F5), I get this error on Flutter 3.16.x |
I found a hacky solution. We are overriding all the exceptions thrown by default. Then we are going to create custom interceptor to handle and throw exceptions. This way we can catch the DioException as we did before.
final baseOptions = BaseOptions(
baseUrl: host,
contentType: Headers.jsonContentType,
validateStatus: (int? status) {
return status != null;
// return status != null && status >= 200 && status < 300;
},
);
final dio = Dio(baseOptions);
class ErrorInterceptor extends Interceptor {
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
final status = response.statusCode;
final isValid = status != null && status >= 200 && status < 300;
if (!isValid) {
throw DioException.badResponse(
statusCode: status!,
requestOptions: response.requestOptions,
response: response,
);
}
super.onResponse(response, handler);
}
}
dio.interceptors.addAll([
ErrorInterceptor(),
]); Link to my gist https://gist.github.com/shakthizen/d644aaf19ca837a4a29a92ebad551055 |
@d1ss0nanz |
The onError handler of the Dio interceptor is not getting called. That’s were the reauthentication is handled (using a refresh token). It worked for a long time and broke recently. |
Any update? update: I did find this solution which it work for me: https://stackoverflow.com/questions/69427612/dioerror-dioerrortype-response-http-status-error-401 |
Seems fixed in the latest version. |
Package
dio
Version
5.4.0
Operating-System
Android
Output of
flutter doctor -v
Dart Version
3.2.2
Steps to Reproduce
I have this Intercepter:
Expected Result
When I call an API that returns a 401, because the token has expired, the onError method should get a new token from the API.
And then its all good
Actual Result
This have worked, is have now stopped working,
Is gets to the "onRequest" in the Interfecpter, and sets the header, but it never gets to the "onError"
and I get a DioException instead:
Its thrown from dio_mixin.dart in the method "_dispatchRequest" -> (Line 564) throw assureDioException(e, reqOpt);
Exception has occurred.
DioException (DioException [bad response]: This exception was thrown because the response has a status code of 401 and RequestOptions.validateStatus was configured to throw for this status code.
The status code of 401 has the following meaning: "Client error - the request contains bad syntax or cannot be fulfilled"
Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
In order to resolve this exception you typically have either to verify and fix your request code or you have to fix the server code.
)
The text was updated successfully, but these errors were encountered: