Skip to content

refreshToken fails with unhandled exception: bad state: The 'handler' has already been called #3

@DigitalSeneca

Description

@DigitalSeneca

Where:

@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
// Check if the error is due to an expired token (401)
if (err.response?.statusCode != 401) {
super.onError(err, handler);
}
try {
// Try to refresh the token
final Response<TokenRefresh> tokenRefreshResponse =
await _authApi.refreshToken(
tokenRefreshRequest: TokenRefreshRequest(
(builder) => builder..refresh = this.refreshToken));
final String? newToken = tokenRefreshResponse.data?.access;
if (newToken != null) {
this.accessToken = newToken;
// Trigger the callback if it's provided
if (onTokenUpdateCallback != null) {
onTokenUpdateCallback!(newToken);
}
}
} catch (_) {
handler.reject(RevokedTokenException(requestOptions: err.requestOptions));
return;
}
try {
// Retry request
err.requestOptions.headers.addAll(await _buildHeaders());
handler.resolve(await _dio.fetch(err.requestOptions));
} catch (_) {
super.onError(err, handler);
return;
}
}

What:

E/flutter (13937): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Bad state: The `handler` has already been called, make sure each handler gets called only once.
E/flutter (13937): #0      _BaseHandler._throwIfCompleted (package:dio/src/interceptor.dart:34:7)
E/flutter (13937): #1      ErrorInterceptorHandler.reject (package:dio/src/interceptor.dart:182:5)
E/flutter (13937): #2      JwtAuthInterceptor.onError (package:mosquito_alert/src/auth/jwt_auth.dart:78:22)
E/flutter (13937): <asynchronous suspension>

Potential solution / thoughts:

handler can only be called once so calling handler.reject and then retrying request will cause calling the handler twice
Maybe we need to return early instead of calling handler.reject

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions