safeFetchV2<T> method

  1. @protected
Future<Either<FetchFailure, T>> safeFetchV2<T>(
  1. Future<T> call()
)

Implementation

@protected
Future<Either<FetchFailure, T>> safeFetchV2<T>(
    Future<T> Function() call) async {
  return safeCall(
    call: call,
    onNetworkError: () => const FetchFailure.networkError(),
    onResponseError: (Response? response) {
      try {
        if (response != null && response.statusCode != null) {
          int? statusCode = response.statusCode ?? 00;
          var msg = response.data["msg"];
          var data = response.data["data"];
          var codeName = response.data["code_name"];
          if (statusCode == 401) {
            return FetchFailure.login([data], data, codeName);
          } else if (statusCode >= 400 && statusCode < 500) {
            try {
              return FetchFailure.warningError(statusCode, [msg], data);
            } catch (e) {
              msg = [response.statusMessage.toString()];
              data = response.data.toString();
              return FetchFailure.warningError(statusCode, msg, data);
            }
          } else if (statusCode >= 500 && statusCode < 600) {
            try {
              return FetchFailure.serverError(statusCode, msg, data);
            } catch (e) {
              msg = [response.statusMessage.toString()];
              data = response.data.toString();
              return FetchFailure.warningError(statusCode, msg, data);
            }
          }
        }
        return FetchFailure.unknownError(101, "Unknown", "Unknown");
      } catch (e) {
        var status = response?.statusCode ?? 101;
        String msg = response?.statusMessage ?? "";
        var data = response?.statusMessage ?? "";
        return FetchFailure.serverError(status, [msg], data);
      }
    },
    onUnknownError: (Exception e) =>
        FetchFailure.unknownError(101, "Unknown", "Unknown"),
  );
}