safeCallMessageStatus<T> method

Future<Either<FetchFailure, String?>> safeCallMessageStatus<T>({
  1. required Either<FetchFailure, BaseObjectResponse<T>> response,
  2. bool isMessageFromData = true,
  3. bool isShowError = true,
})

Implementation

Future<Either<FetchFailure, String?>> safeCallMessageStatus<T>(
    {required Either<FetchFailure, BaseObjectResponse<T>> response,
    bool isMessageFromData = true,
    bool isShowError = true}) async {
  try {
    if (response.get != null) {
      if (response.get?.success == true) {
        if (isMessageFromData) {
          return response.map((data) => data.data.toString());
        } else {
          return response.map((data) => data.message.toString());
        }
      } else {
        final msg = response.get?.message.toString() ?? "Error";
        final data = response.get?.data.toString() ?? "Error";
        return left(await safeResponseError(
            typeError: TypeError.ERROR_WARNING,
            errorServer: FetchFailure.warningError(200, [msg], data),
            isShowError: isShowError));
      }
    } else {
      return left(await safeResponseError(
          typeError: TypeError.ERROR_SERVER,
          errorServer: response.leftOrCrash,
          isShowError: isShowError));
    }
  } catch (e) {
    logger.d(e.toString());
    final failure = returnFailure(e);
    return left(await safeResponseError(
        typeError: TypeError.ERROR_UNKNOWN,
        errorServer: FetchFailure.crashError(100, failure, "Error"),
        isShowError: isShowError));
  }
}