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

Fix Issue with Retrying Failed Request After Successful Token Refresh #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ahmtydn
Copy link

@ahmtydn ahmtydn commented Sep 28, 2024

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:

  1. 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.

  2. Request Cancellation and Callback: When an error occurs after the Refresh Token process, the request is canceled using error.requestOptions.cancelToken?.cancel() and the onRefreshFail callback 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 cancelToken and onRefreshFail callbacks 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 onRefreshFail function is successfully triggered.

Fixes #118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect Error Handling After Successful Token Refresh
1 participant