From 4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 9 Jan 2023 20:20:09 -0300 Subject: fix #7153: more error handling if handler do not trap error then fail at compile time, all safe handlers push alert on error errors are typed so they render good information --- .../src/cta/Payment/state.ts | 69 +++++++++++----------- 1 file changed, 33 insertions(+), 36 deletions(-) (limited to 'packages/taler-wallet-webextension/src/cta/Payment/state.ts') diff --git a/packages/taler-wallet-webextension/src/cta/Payment/state.ts b/packages/taler-wallet-webextension/src/cta/Payment/state.ts index 6d7ef6b20..0f1388ea5 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/state.ts +++ b/packages/taler-wallet-webextension/src/cta/Payment/state.ts @@ -19,11 +19,10 @@ import { ConfirmPayResultType, NotificationType, PreparePayResultType, - TalerErrorCode, } from "@gnu-taler/taler-util"; -import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core"; -import { useEffect, useState } from "preact/hooks"; -import { alertFromError } from "../../context/alert.js"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { useEffect } from "preact/hooks"; +import { alertFromError, useAlertContext } from "../../context/alert.js"; import { useBackendContext } from "../../context/backend.js"; import { useTranslationContext } from "../../context/translation.js"; import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js"; @@ -36,7 +35,7 @@ export function useComponentState({ goToWalletManualWithdraw, onSuccess, }: Props): State { - const [payErrMsg, setPayErrMsg] = useState(undefined); + const { pushAlertOnError } = useAlertContext(); const api = useBackendContext(); const { i18n } = useTranslationContext(); @@ -142,43 +141,41 @@ export function useComponentState({ } async function doPayment(): Promise { - try { - if (payStatus.status !== "payment-possible") { - throw TalerError.fromUncheckedDetail({ - code: TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR, - hint: `payment is not possible: ${payStatus.status}`, - }); - } - const res = await api.wallet.call(WalletApiOperation.ConfirmPay, { - proposalId: payStatus.proposalId, - }); - // handle confirm pay - if (res.type !== ConfirmPayResultType.Done) { - throw TalerError.fromUncheckedDetail({ - code: TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR, - hint: `could not confirm payment`, - payResult: res, - }); - } - const fu = res.contractTerms.fulfillment_url; - if (fu) { - if (typeof window !== "undefined") { - document.location.href = fu; - } else { - console.log(`should d to ${fu}`); - } - } + // if (payStatus.status !== "payment-possible") { + // throw TalerError.fromUncheckedDetail({ + // code: TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR, + // when: new Date().toISOString(), + // hint: `payment is not possible: ${payStatus.status}`, + // }); + // } + const res = await api.wallet.call(WalletApiOperation.ConfirmPay, { + proposalId: payStatus.proposalId, + }); + // handle confirm pay + if (res.type !== ConfirmPayResultType.Done) { + // throw new BackgroundError("Could not confirm payment", res.lastError) + // // throw TalerError.fromUncheckedDetail({ + // // code: TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR, + // // when: new Date().toISOString(), + // // hint: `could not confirm payment`, + // // payResult: res, + // // }); onSuccess(res.transactionId); - } catch (e) { - if (e instanceof TalerError) { - setPayErrMsg(e); + return; + } + const fu = res.contractTerms.fulfillment_url; + if (fu) { + if (typeof window !== "undefined") { + document.location.href = fu; + } else { + console.log(`should d to ${fu}`); } } + onSuccess(res.transactionId); } const payHandler: ButtonHandler = { - onClick: payErrMsg ? undefined : doPayment, - error: payErrMsg, + onClick: pushAlertOnError(doPayment), }; // (payStatus.status === PreparePayResultType.PaymentPossible) -- cgit v1.2.3