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();
      // AnalyticsService.logApiErrorEvent(
      //   errorCode: error.response?.statusCode?.toString() ?? "",
      //   errorMessage: (error.response?.data == null)
      //       ? ""
      //       : (json.decode(
      //               json.encode(error.response?.data))["errorMessage"] ??
      //           ""),
      //   path: path,
      // );
      return APIException(message: error.response?.statusMessage ?? "");
    }
  } else {
    return APIException(message: ErrorMessages.networkGeneral);
  }
}