diff options
author | Sebastian <sebasjm@gmail.com> | 2024-03-04 11:52:36 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-03-04 11:52:36 -0300 |
commit | c58349ebaa173881a4201e83e928325707758eb8 (patch) | |
tree | a5cf60762675072f7f2b6edbe1d12568aebc7e74 /packages/taler-wallet-webextension/src/context | |
parent | 97d4a6445ad86ffad842fd763e0ac8053b971e69 (diff) | |
download | wallet-core-c58349ebaa173881a4201e83e928325707758eb8.tar.xz |
fix #8371
Diffstat (limited to 'packages/taler-wallet-webextension/src/context')
-rw-r--r-- | packages/taler-wallet-webextension/src/context/alert.ts | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/packages/taler-wallet-webextension/src/context/alert.ts b/packages/taler-wallet-webextension/src/context/alert.ts index 1ae15f1ec..45fc9d9d9 100644 --- a/packages/taler-wallet-webextension/src/context/alert.ts +++ b/packages/taler-wallet-webextension/src/context/alert.ts @@ -19,13 +19,14 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { TalerErrorDetail, TranslatedString } from "@gnu-taler/taler-util"; +import { TalerError, TalerErrorCode, TalerErrorDetail, TranslatedString } from "@gnu-taler/taler-util"; import { ComponentChildren, createContext, h, VNode } from "preact"; import { useContext, useState } from "preact/hooks"; import { HookError } from "../hooks/useAsyncAsHook.js"; import { SafeHandler, withSafe } from "../mui/handlers.js"; import { BackgroundError } from "../wxApi.js"; -import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { InternationalizationAPI, useTranslationContext } from "@gnu-taler/web-util/browser"; +import { platform } from "../platform/foreground.js"; export type AlertType = "info" | "warning" | "error" | "success"; @@ -102,24 +103,24 @@ export const AlertProvider = ({ children }: Props): VNode => { setAlerts((ns: AlertWithDate[]) => ns.filter((n) => n !== alert)); }; + const { i18n } = useTranslationContext(); + function pushAlertOnError<T>( handler: (p: T) => Promise<void>, ): SafeHandler<T> { return withSafe(handler, (e) => { - const a = alertFromError(e.message as TranslatedString, e); + const a = alertFromError(i18n, e.message as TranslatedString, e); pushAlert(a); }); } - const { i18n } = useTranslationContext(); - function safely<T>( name: string, handler: (p: T) => Promise<void>, ): SafeHandler<T> { const message = i18n.str`Error was thrown trying to: "${name}"`; return withSafe(handler, (e) => { - const a = alertFromError(message, e); + const a = alertFromError(i18n, message, e); pushAlert(a); }); } @@ -133,24 +134,28 @@ export const AlertProvider = ({ children }: Props): VNode => { export const useAlertContext = (): Type => useContext(Context); export function alertFromError( + i18n: InternationalizationAPI, message: TranslatedString, error: HookError, ...context: any[] ): ErrorAlert; export function alertFromError( + i18n: InternationalizationAPI, message: TranslatedString, error: Error, ...context: any[] ): ErrorAlert; export function alertFromError( + i18n: InternationalizationAPI, message: TranslatedString, error: TalerErrorDetail, ...context: any[] ): ErrorAlert; export function alertFromError( + i18n: InternationalizationAPI, message: TranslatedString, error: HookError | TalerErrorDetail | Error, ...context: any[] @@ -170,14 +175,23 @@ export function alertFromError( //HookError description = error.message as TranslatedString; if (error.type === "taler") { + const msg = isWalletNotAvailable(i18n,error.details) + if (msg) { + description = msg + } cause = { details: error.details, }; } } else { if (error instanceof BackgroundError) { - description = (error.errorDetail.hint ?? - `Error code: ${error.errorDetail.code}`) as TranslatedString; + const msg = isWalletNotAvailable(i18n,error.errorDetail) + if (msg) { + description = msg + } else { + description = (error.errorDetail.hint ?? + `Error code: ${error.errorDetail.code}`) as TranslatedString; + } cause = { details: error.errorDetail, stack: error.stack, @@ -202,3 +216,21 @@ export function alertFromError( context, }; } + +function isWalletNotAvailable(i18n: InternationalizationAPI, detail: TalerErrorDetail): TranslatedString | undefined { + if (detail.code === TalerErrorCode.WALLET_CORE_NOT_AVAILABLE + && detail.lastError) { + const le = detail.lastError as TalerErrorDetail + if (le.code === TalerErrorCode.WALLET_DB_UNAVAILABLE) { + if (platform.isFirefox()) { + return i18n.str`Could not open the wallet database. Firefox is known to run into this problem under "permanent private mode".` + } else { + return i18n.str`Could not open the wallet database.` + } + } else { + return (detail.hint ?? `Error code: ${detail.code}`) as TranslatedString; + } + + } + return undefined +} |