onError method

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

The callback will be executed on error.

If you want to continue the error , call handler.next.

If you want to complete the response with some custom data directly, you can resolve a Response object with handler.resolve and other error interceptor(s) will be skipped.

If you want to complete the response with an error message directly, you can reject a DioError object with handler.reject, and other error interceptor(s) will be skipped.

Implementation

@override
void onError(DioError err, ErrorInterceptorHandler handler) {
  EventTracker().addEvent(getEvent(Events.RESPONSE_VALIDATION, EventTracker.sourceSdk));

  if(err.response != null) {
    var ddHeaders = err.response?.headers.map.containsKey('x-dd-b');
    var code = err.response!.statusCode;
    if ((code == 401 || code == 403) && (ddHeaders != null && ddHeaders)) {
      //blocked by datadome
      failedRequests.add(RetriableRequest(dio, err, handler));
      if (CaptchaPage.isDisplayed) {
        return;
      }

      var captchaUrl = err.response?.data['url'];
      if(captchaUrl == null || captchaUrl.trim().isEmpty) {
        DataDomeLogger.warning("Missing or empty captcha URL in response body");
        return;
      }

      if (context != null) {
        showCaptcha(
            context!,
            captchaUrl,
                (String cookie) {
              didResolveCaptcha(cookie);
            }
        );
      } else {
        Widget widget = new CaptchaPage(captchaUrl: captchaUrl,
            onCaptchaResolved: (String cookie) {
              didResolveCaptcha(cookie);
            });
        if (onCaptchaDisplay != null) {
          onCaptchaDisplay!(widget);
        }
      }
      return;
    }
  }

  super.onError(err, handler);
}