onError method

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

Called when an exception was occurred during the request.

Implementation

@override
Future<void> onError(
  DioException err,
  ErrorInterceptorHandler handler,
) async {
  final options = err.requestOptions;
  final eligible = options.extra['offlineSync'] == true;
  final isReplay = options.extra['isSyncRequest'] == true;
  final isMutation = const <String>{
    'POST',
    'PUT',
    'DELETE',
    'PATCH',
  }.contains(options.method.toUpperCase());

  final unscopedAuth =
      options.extra['withAuth'] == true && currentSession().isAnonymous;
  if (!isReplay &&
      eligible &&
      isMutation &&
      !unscopedAuth &&
      LikeSessionIdentity.isCurrent(options) &&
      _isNetworkError(err)) {
    try {
      await _queueRequest(options);
    } catch (error, stack) {
      handler.next(
        DioException(
          requestOptions: options,
          type: err.type,
          error: error,
          stackTrace: stack,
          message: 'Unable to persist offline action',
        ),
      );
      return;
    }
    handler.reject(
      DioException(
        requestOptions: options,
        error: 'OFFLINE_QUEUED',
        type: DioExceptionType.unknown,
      ),
    );
    return;
  }
  handler.next(err);
}