From 9922192b0dba2e479b5af3e29c1d44b98e4d29d7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 28 Feb 2023 19:03:43 -0300 Subject: fix #7729 --- packages/demobank-ui/src/utils.ts | 64 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'packages/demobank-ui/src/utils.ts') diff --git a/packages/demobank-ui/src/utils.ts b/packages/demobank-ui/src/utils.ts index 49b9ac276..81dd450a4 100644 --- a/packages/demobank-ui/src/utils.ts +++ b/packages/demobank-ui/src/utils.ts @@ -14,7 +14,13 @@ GNU Taler; see the file COPYING. If not, see */ -import { canonicalizeBaseUrl } from "@gnu-taler/taler-util"; +import { HttpStatusCode, TranslatedString } from "@gnu-taler/taler-util"; +import { + ErrorType, + HttpError, + useTranslationContext, +} from "@gnu-taler/web-util/lib/index.browser"; +import { ErrorMessage } from "./context/pageState.js"; /** * Validate (the number part of) an amount. If needed, @@ -58,6 +64,13 @@ export type WithIntermediate = { ? WithIntermediate : Type[prop] | undefined; }; +export type RecursivePartial = { + [P in keyof T]?: T[P] extends (infer U)[] + ? RecursivePartial[] + : T[P] extends object + ? RecursivePartial + : T[P]; +}; export enum TanChannel { SMS = "sms", @@ -99,3 +112,52 @@ export enum CashoutStatus { export const PAGE_SIZE = 20; export const MAX_RESULT_SIZE = PAGE_SIZE * 2 - 1; + +export function buildRequestErrorMessage( + i18n: ReturnType["i18n"], + cause: HttpError, + specialCases: { + onClientError?: (status: HttpStatusCode) => TranslatedString | undefined; + onServerError?: (status: HttpStatusCode) => TranslatedString | undefined; + } = {}, +): ErrorMessage { + let result: ErrorMessage; + switch (cause.type) { + case ErrorType.TIMEOUT: { + result = { + title: i18n.str`Request timeout`, + }; + break; + } + case ErrorType.CLIENT: { + const title = + specialCases.onClientError && specialCases.onClientError(cause.status); + result = { + title: title ? title : i18n.str`The server didn't accept the request`, + description: cause.payload.error.description, + debug: JSON.stringify(cause), + }; + break; + } + case ErrorType.SERVER: { + const title = + specialCases.onServerError && specialCases.onServerError(cause.status); + result = { + title: title + ? title + : i18n.str`The server had problems processing the request`, + description: cause.payload.error.description, + debug: JSON.stringify(cause), + }; + break; + } + case ErrorType.UNEXPECTED: { + result = { + title: i18n.str`Unexpected error`, + debug: JSON.stringify(cause), + }; + break; + } + } + return result; +} -- cgit v1.2.3