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/TransferCreate/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/TransferCreate/state.ts')
| -rw-r--r-- | packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts index ecea53848..6574d6ba1 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/state.ts @@ -22,7 +22,7 @@ import { 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"; @@ -34,16 +34,13 @@ export function useComponentState({ onSuccess, }: Props): State { const api = useBackendContext(); + const { pushAlertOnError } = useAlertContext(); const amount = Amounts.parseOrThrow(amountStr); const { i18n } = useTranslationContext(); const [subject, setSubject] = useState<string | undefined>(); const [timestamp, setTimestamp] = useState<string | undefined>(); - const [operationError, setOperationError] = useState< - TalerErrorDetail | undefined - >(undefined); - const hook = useAsyncAsHook(async () => { const resp = await api.wallet.call( WalletApiOperation.PreparePeerPushPayment, @@ -104,25 +101,17 @@ export function useComponentState({ async function accept(): Promise<void> { if (!subject || !purse_expiration) return; - try { - const resp = await api.wallet.call( - WalletApiOperation.InitiatePeerPushPayment, - { - partialContractTerms: { - summary: subject, - amount: amountStr, - purse_expiration, - }, + const resp = await api.wallet.call( + WalletApiOperation.InitiatePeerPushPayment, + { + partialContractTerms: { + summary: subject, + amount: amountStr, + 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 = @@ -131,7 +120,7 @@ export function useComponentState({ return { status: "ready", cancel: { - onClick: onClose, + onClick: pushAlertOnError(onClose), }, subject: { error: @@ -141,21 +130,20 @@ export function useComponentState({ ? "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); - }, + }), }, create: { - onClick: unableToCreate ? undefined : accept, + onClick: unableToCreate ? undefined : pushAlertOnError(accept), }, debitAmount, toBeReceived, error: undefined, - operationError, }; } |
