diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/mui')
-rw-r--r-- | packages/taler-wallet-webextension/src/mui/handlers.ts | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/packages/taler-wallet-webextension/src/mui/handlers.ts b/packages/taler-wallet-webextension/src/mui/handlers.ts index 655fceef9..61786742f 100644 --- a/packages/taler-wallet-webextension/src/mui/handlers.ts +++ b/packages/taler-wallet-webextension/src/mui/handlers.ts @@ -14,23 +14,51 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ import { AmountJson } from "@gnu-taler/taler-util"; -import { TalerError } from "@gnu-taler/taler-wallet-core"; export interface TextFieldHandler { - onInput?: (value: string) => Promise<void>; + onInput?: SafeHandler<string>; value: string; error?: string; } export interface AmountFieldHandler { - onInput?: (value: AmountJson) => Promise<void>; + onInput?: SafeHandler<AmountJson>; value: AmountJson; error?: string; } +declare const __safe_handler: unique symbol; +export type SafeHandler<T> = { + <Req extends T>(req: Req): Promise<void>; + (): Promise<void>; + [__safe_handler]: true; +}; + +export function withSafe<T>( + handler: (p: T) => Promise<void>, + onError: (e: Error) => void, +): SafeHandler<T> { + const sh = async function (p: T): Promise<void> { + try { + await handler(p); + } catch (e) { + if (e instanceof Error) { + onError(e); + } else { + onError(new Error(String(e))); + } + } + }; + return sh as SafeHandler<T>; +} + +export const nullFunction = async function (): Promise<void> { + //do nothing +} as SafeHandler<void>; + export interface ButtonHandler { - onClick?: () => Promise<void>; - error?: TalerError; + onClick?: SafeHandler<void>; + // error?: TalerError; } export interface ToggleHandler { @@ -39,7 +67,7 @@ export interface ToggleHandler { } export interface SelectFieldHandler { - onChange?: (value: string) => Promise<void>; + onChange?: SafeHandler<string>; error?: string; value: string; isDirty?: boolean; |