onError method

  1. @override
void onError(
  1. DioError dioError,
  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
onError(DioError dioError, ErrorInterceptorHandler handler) {
  String errorString = '';

  if (dioError.response != null) {
    if (dioError.response!.statusCode == 422) {
      errorString = 'Input validation error: 422';
    } else if (dioError.response!.statusCode == 400) {
      errorString = 'Bad request: 400 ';
    } else if (dioError.response!.statusCode == 403) {
      errorString = 'UnAuth request: 403 ';
    } else if (dioError.response!.statusCode == 401) {
      errorString = 'You are not authorised to perform this action: 401';
    } else if (dioError.response!.statusCode == 500) {
      errorString = 'An internal error occurred: 500';
    } else if (dioError.response!.statusCode == 404) {
      errorString = 'An internal error occurred: 404';
    } else {
      errorString = 'An error occurred completing your request, ';
    }
  } else {
    errorString = 'Please check your internet and try again';
    // call  bugsnag.
  }

  if (dioError.response == null) {
    dioError.error = RequestResponse._(
      status: 'failed',
      statusCode: 0,
      error: 'Error: $errorString',
    );
  } else {
    dioError.error = RequestResponse._(
      status: 'failed',
      statusCode: dioError.response!.statusCode,
      error: 'Error: $errorString',
    );
  }

  return super.onError(dioError, handler);
}