aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/utils.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-02-28 19:03:43 -0300
committerSebastian <sebasjm@gmail.com>2023-02-28 19:03:43 -0300
commit9922192b0dba2e479b5af3e29c1d44b98e4d29d7 (patch)
tree260f2836892b93188bf17e30b2024ccea21262dd /packages/demobank-ui/src/utils.ts
parent740849dd89e3746fdc34c3a112288dbfe4bd7220 (diff)
downloadwallet-core-9922192b0dba2e479b5af3e29c1d44b98e4d29d7.tar.xz
fix #7729
Diffstat (limited to 'packages/demobank-ui/src/utils.ts')
-rw-r--r--packages/demobank-ui/src/utils.ts64
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;
+}