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 {
  final requestOptions = err.requestOptions;
  final retryCount = requestOptions.extra['retry_count'] as int? ?? 0;

  if (retryCount < maxRetries && retryableErrors.contains(err.type)) {
    requestOptions.extra['retry_count'] = retryCount + 1;

    if (LokotroPayEnv.debugMode) {
      debugPrint('[Lokotro Pay Retry] Retrying request ${retryCount + 1}/$maxRetries');
    }

    // Wait before retry
    await Future.delayed(retryDelay * (retryCount + 1));

    try {
      final response = await Dio().fetch(requestOptions);
      handler.resolve(response);
      return;
    } catch (e) {
      // Continue to next retry or fail
    }
  }

  super.onError(err, handler);
}