onError method

  1. @override
void onError(
  1. DioException err,
  2. ErrorInterceptorHandler handler
)

Called when an exception was occurred during the request.

Implementation

@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
  // Check if the user is unauthorized.
  if (err.response?.statusCode == 401) {
    // Refresh the user's authentication token.
    await _authenticationRepository.refreshToken();
    // Retry the request.
    try {
      handler.resolve(await _retry(err.requestOptions));
    } on DioException catch (e) {
      // If the request fails again, pass the error to the next interceptor in the chain.
      handler.next(e);
    }
    // Return to prevent the next interceptor in the chain from being executed.
    return;
  }
  // Pass the error to the next interceptor in the chain.
  handler.next(err);
}