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 {
  try {
    final tracker = _activeTrackers.remove(
      err.requestOptions.extra[_trackerId],
    );
    if (tracker != null) {
      final statusCode = err.response?.statusCode;
      if (statusCode != null) {
        // TODO: Find a way to test this (coverage).
        // Errors for when status code is not in accepted range should be recorded as normal
        await tracker.setResponseStatusCode(statusCode);
        await _logResponse(err.response, tracker);
      } else {
        // If status code is null, it means that the error is not a response, but rather a network error
        await tracker.setError(err.toString(), err.stackTrace.toString());
      }
      await tracker.reportDone();
    }
  } finally {
    handler.next(err);
  }
}