onError method

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

Emits an 'error' debug event containing the full error context.

The data map includes the error type, message, status code (if a response was received), the originating request options, the response body, and the stack trace.

Note: HTTP 4xx/5xx responses are handled by Connector.send and never reach this method because Dio is configured with validateStatus: (_) => true. Only network-level errors (connection failures, timeouts, etc.) trigger this callback.

Implementation

@override
void onError(DioException err, ErrorInterceptorHandler handler) {
  onDebug(
    event: 'error',
    message: '${err.type}: ${err.message}',
    data: {
      'type': err.type.toString(),
      'message': err.message,
      'statusCode': err.response?.statusCode,
      'requestOptions': {
        'method': err.requestOptions.method,
        'url': err.requestOptions.uri.toString(),
      },
      'response': err.response?.data,
      'stackTrace': err.stackTrace.toString(),
    },
  );

  super.onError(err, handler);
}