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/InvoiceCreate/state.ts | |
parent | 8a70edb2f8e235c3462127b0aa4e1b65aa1aee0b (diff) |
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/InvoiceCreate/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts index 7dcda4c52..ee5375859 100644 --- a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts +++ b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/state.ts @@ -15,15 +15,11 @@ */ /* eslint-disable react-hooks/rules-of-hooks */ -import { - Amounts, - TalerErrorDetail, - TalerProtocolTimestamp, -} from "@gnu-taler/taler-util"; +import { Amounts, TalerProtocolTimestamp } from "@gnu-taler/taler-util"; import { TalerError, WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { isFuture, parse } from "date-fns"; import { useState } from "preact/hooks"; -import { alertFromError } from "../../context/alert.js"; +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"; @@ -71,6 +67,7 @@ export function useComponentState({ return () => { const [subject, setSubject] = useState<string | undefined>(); const [timestamp, setTimestamp] = useState<string | undefined>(); + const { pushAlertOnError } = useAlertContext(); const selectedExchange = useSelectedExchange({ currency: amount.currency, @@ -144,27 +141,20 @@ export function useComponentState({ async function accept(): Promise<void> { if (!subject || !purse_expiration) return; - try { - const resp = await api.wallet.call( - WalletApiOperation.InitiatePeerPullPayment, - { - exchangeBaseUrl: exchange.exchangeBaseUrl, - partialContractTerms: { - amount: Amounts.stringify(amount), - summary: subject, - purse_expiration, - }, + + const resp = await api.wallet.call( + WalletApiOperation.InitiatePeerPullPayment, + { + exchangeBaseUrl: exchange.exchangeBaseUrl, + partialContractTerms: { + amount: Amounts.stringify(amount), + summary: subject, + purse_expiration, }, - ); + }, + ); - onSuccess(resp.transactionId); - } catch (e) { - if (e instanceof TalerError) { - // setOperationError(e.errorDetail); - } - console.error(e); - throw Error("error trying to accept"); - } + onSuccess(resp.transactionId); } const unableToCreate = !subject || Amounts.isZero(amount) || !purse_expiration; @@ -176,25 +166,25 @@ export function useComponentState({ subject === undefined ? undefined : !subject - ? "Can't be empty" - : undefined, + ? "Can't be empty" + : undefined, value: subject ?? "", - onInput: async (e) => setSubject(e), + onInput: pushAlertOnError(async (e) => setSubject(e)), }, expiration: { error: timestampError, value: timestamp === undefined ? "" : timestamp, - onInput: async (e) => { + onInput: pushAlertOnError(async (e) => { setTimestamp(e); - }, + }), }, doSelectExchange: selectedExchange.doSelect, exchangeUrl: exchange.exchangeBaseUrl, create: { - onClick: unableToCreate ? undefined : accept, + onClick: unableToCreate ? undefined : pushAlertOnError(accept), }, cancel: { - onClick: onClose, + onClick: pushAlertOnError(onClose), }, requestAmount, toBeReceived, |