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 response = err.response;

  if (kDebugMode) {
    print("Api: ${err.requestOptions.path}");
    print("Request Params: ${err.requestOptions.data}");
    print("Api Response: ${response?.statusCode}");
    print("Response: ${response?.data}");
  }

  //THis for handling network connection
  if (_shouldRetry(err)) {
    try {
      Response res =
          await requestRetrier.scheduleRequestRetry(err.requestOptions);
      return handler.resolve(res);
    } catch (e) {
      // Let any new error from the retrier pass through
      //handler.next(e);
      //return;
    }
  }
  //print("status code ${response!.statusCode} and data= ${response.data}");
  if (response == null) {
    handler.next(err);
    return;
  }

  if (response.statusCode == 400) {
    var data = response.data;
    try {
      if (data['errorcode'] == 8601 &&
          data['reason'] == "Limit Reached. Please Try After Some Time") {
        return handler.resolve(response);
        //return handler.resolve(await onJWTExpired(err, handler));
      } else if (data['errorcode'] == 6002 &&
          data['reason'] == "Invalid Credential") {
        return handler.resolve(response);
      } else if (data['errorcode'] == 6110 &&
          (data['reason'] == "Verification Pending" ||
              data['reason'] == "Email Verification Pending")) {
        return handler.resolve(response);
      } else if (data['errorcode'] == 8000 &&
          data['reason'] == "This Event has Expired") {
        return handler.resolve(response);
      } else if (data['errorcode'] == 8003 &&
          data['reason'] == "Booking Slot not available") {
        return handler.resolve(response);
      } else if (data['errorcode'] ==
              6001 /*&&
          data['reason'] == "Invalid User"*/
          ) {
        return handler.resolve(response);
      } else if (data['errorcode'] == 6401 &&
          data['reason'] == 'Mobile Number Not Valid') {
        return handler.resolve(response);
      } else if (data['errorcode'] == 6005 &&
          data['reason'] == "Invalid Otp") {
        return handler.resolve(response);
      } else if (data['errorcode'] == 6111) {
        return handler.resolve(response);
      }
    } catch (keyerror) {
      //do nothing here
      //print("=>> keyerror $keyerror");
    }
  } else if (response.statusCode == 401) {
    var data = response.data;
    //RequestOptions options = err.requestOptions;

    //print("on 401 response =>");
    //print("response data is =... ${response.data}");
    //print("request data => ${options.data}");
    //print("request headers => ${options.headers}");
    //print("resues url => ${options.path}");
    /*
    {errorcode: 6066, reason: Cannot read property 'payload' of null}
    */
    try {
      if (data['errorcode'] == 6055 && data['reason'] == "jwt expired") {
        return handler.resolve(await onJWTExpired(err, handler));
      }
    } catch (keyerror) {
      //do nothing here
      //print("=>> keyerror $keyerror");
    }
    return handler.next(err);
  } else {
    // RequestOptions options = err.requestOptions;

    if (kDebugMode) {
      // print("on ${response.statusCode} response =>");
      // print("response data is =... ${response.data}");
      // print("request data => ${options.data}");
      // print("request headers => ${options.headers}");
    }
  }
  return handler.resolve(err.response!);
}