handleError static method

APIException handleError({
  1. required Exception error,
  2. required String path,
  3. bool showException = true,
})

Implementation

static APIException handleError({
  required Exception error,
  required String path,
  bool showException = true,
}) {
  if (error is DioException) {
    if (error.type == DioExceptionType.connectionError) {
      // Fluttertoast.showToast(msg: "No internet connection");
      return APIException(message: "No internet connection");
    } else {
      String? errorMessage =
          json.decode(json.encode(error.response?.data))?["errorMessage"];
      String? errorText =
          json.decode(json.encode(error.response?.data))?["error"];
      if ((errorMessage != null || errorText != null) &&
          (error.response?.statusCode != 401) &&
          showException) {
        Fluttertoast.showToast(
          msg: errorMessage ??
              ((errorText == "invalid_grant")
                  ? ErrorMessages.otpIsInvalid
                  : (errorText ?? "")),
        );
      }
      LoadingUtils.hideLoader();
      String errorCode = error.response?.statusCode?.toString() ?? "";
      VoucherAnalyticsImpl.logEvent(
        eventName: "api_error_$errorCode",
        parameters: {
          "error_message": errorMessage?.toString() ?? "",
          "error_code": errorCode,
          "api_url": path,
        },
      );
      return APIException(message: error.response?.statusMessage ?? "");
    }
  } else {
    return APIException(message: ErrorMessages.networkGeneral);
  }
}