diff options
Diffstat (limited to 'packages/demobank-ui/src/utils.ts')
-rw-r--r-- | packages/demobank-ui/src/utils.ts | 64 |
1 files changed, 63 insertions, 1 deletions
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 <http://www.gnu.org/licenses/> */ -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<Type extends object> = { ? WithIntermediate<Type[prop]> : Type[prop] | undefined; }; +export type RecursivePartial<T> = { + [P in keyof T]?: T[P] extends (infer U)[] + ? RecursivePartial<U>[] + : T[P] extends object + ? RecursivePartial<T[P]> + : 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<typeof useTranslationContext>["i18n"], + cause: HttpError<SandboxBackend.SandboxError>, + 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; +} |