onError method

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

Intercepts API request errors and sends them to Sentry if enabled.

If Sentry tracking is enabled (useSentry is true), errors with status codes >= 400 are reported to Sentry. If an errorExclude function is provided, it is used to filter errors before reporting.

Implementation

@override
void onError(DioException err, ErrorInterceptorHandler handler) {
  // If Sentry is not enabled, pass the error to the next handler.
  if (!(config?.useSentry ?? false)) return super.onError(err, handler);
  if (err.response?.statusCode == null) return super.onError(err, handler);

  // Handle error based on exclusion rules
  if (config?.errorExclude == null) {
    if (err.response!.statusCode! >= 400) {
      Sentry.configureScope((scope) => _configureScope(scope, err, handler));
    }
    return super.onError(err, handler);
  }

  if (config!.errorExclude!(err, handler)) {
    Sentry.configureScope((scope) => _configureScope(scope, err, handler));
  }

  super.onError(err, handler);
}