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 | |
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/TransferCreate')
4 files changed, 21 insertions, 43 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/index.ts b/packages/taler-wallet-webextension/src/cta/TransferCreate/index.ts index b191b4efa..654b03b7f 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/index.ts +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/index.ts @@ -54,7 +54,6 @@ export namespace State { subject: TextFieldHandler; expiration: TextFieldHandler; error: undefined; - operationError?: TalerErrorDetail; } } 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, }; } diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/stories.tsx b/packages/taler-wallet-webextension/src/cta/TransferCreate/stories.tsx index d0650f562..57409bde5 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/stories.tsx +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/stories.tsx @@ -19,14 +19,15 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { createExample } from "../../test-utils.js"; +import { tests } from "@gnu-taler/web-util/lib/index.browser"; +import { nullFunction } from "../../mui/handlers.js"; import { ReadyView } from "./views.js"; export default { title: "transfer create", }; -export const Ready = createExample(ReadyView, { +export const Ready = tests.createExample(ReadyView, { debitAmount: { currency: "ARS", value: 1, @@ -44,8 +45,6 @@ export const Ready = createExample(ReadyView, { }, subject: { value: "the subject", - onInput: async () => { - null; - }, + onInput: nullFunction, }, }); diff --git a/packages/taler-wallet-webextension/src/cta/TransferCreate/views.tsx b/packages/taler-wallet-webextension/src/cta/TransferCreate/views.tsx index cee61b3b8..373af8f74 100644 --- a/packages/taler-wallet-webextension/src/cta/TransferCreate/views.tsx +++ b/packages/taler-wallet-webextension/src/cta/TransferCreate/views.tsx @@ -32,8 +32,6 @@ export function ReadyView({ toBeReceived, debitAmount, create, - operationError, - cancel, }: State.Ready): VNode { const { i18n } = useTranslationContext(); @@ -65,12 +63,6 @@ export function ReadyView({ <SubTitle> <i18n.Translate>Digital cash transfer</i18n.Translate> </SubTitle> - {operationError && ( - <ErrorTalerOperation - title={i18n.str`Could not finish the transfer creation`} - error={operationError} - /> - )} <section style={{ textAlign: "left" }}> <p> <TextField |