Fix Issue with Retrying Failed Request After Successful Token Refresh
Problem:
In the application, after successfully obtaining a Refresh Token, a new request is automatically sent with the refreshed token. However, if a different error (such as 500 Internal Server Error) is encountered during this second request, instead of an authentication error (401 Unauthorized), the original authentication error from the first request is mistakenly thrown again. This prevents handling different errors correctly after the Refresh Token process.
Solution:
This PR ensures that if a different error occurs during the second request after a successful token refresh, that error is properly caught and handled. If a new error is encountered, the original authentication error is not thrown again, and the new error is raised.
Changes Made:
-
New Error Handling Added: If a new error occurs during the request after the Refresh Token process, it is handled with
handler.next(err), and the previous error is not reused. -
Request Cancellation and Callback: When an error occurs after the Refresh Token process, the request is canceled using
error.requestOptions.cancelToken?.cancel()and theonRefreshFailcallback is triggered. This ensures the user is informed about the failure of the Refresh Token process.
Code Example:
} catch (err) {
/// cancel request & call onRefreshFail callback and unlock
error.requestOptions.cancelToken?.cancel();
parameters.onRefreshFail?.call();
/// If error is DioException, then return error
if (err is DioException) {
return handler.next(err);
}
/// If error is not DioException, then return the original error
return handler.next(exception);
}
With This Change:
- If a different error (other than 401 Unauthorized) occurs during the request after a successful token refresh, it is correctly handled and the original error is not reused.
- The
cancelTokenandonRefreshFailcallbacks function correctly during error management and request cancellation.
Tested Scenarios:
- Scenarios where requests succeed after a successful Refresh Token process.
- Scenarios where different errors are encountered after the Refresh Token process, and these errors are correctly handled.
- Scenarios where the
onRefreshFailfunction is successfully triggered.
Fixes #118