diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-09 20:20:09 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-09 20:20:09 -0300 |
commit | 4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7 (patch) | |
tree | 5c16976f99eb973ff62d78ed64107ca01df57b99 /packages/taler-wallet-webextension/src/cta/Payment/state.ts | |
parent | 8a70edb2f8e235c3462127b0aa4e1b65aa1aee0b (diff) | |
download | wallet-core-4a781bd0dd8828ce152f6ab2c3f1bbd6b5e826f7.tar.xz |
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
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Payment/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/Payment/state.ts | 69 |
1 files changed, 33 insertions, 36 deletions
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<TalerError | undefined>(undefined); + const { pushAlertOnError } = useAlertContext(); const api = useBackendContext(); const { i18n } = useTranslationContext(); @@ -142,43 +141,41 @@ export function useComponentState({ } async function doPayment(): Promise<void> { - 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) |