aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-10-30 15:27:25 -0300
committerSebastian <sebasjm@gmail.com>2023-10-30 15:27:25 -0300
commit768838285c25cbb1b171f645e8efb37a3c14273a (patch)
tree3404a7ea452a357baf4ebfc6c3b400f601849744 /packages/web-util/src
parentb7ba3119c1ff0d9ae3432cf0de1ef8cf92fc193c (diff)
downloadwallet-core-768838285c25cbb1b171f645e8efb37a3c14273a.tar.xz
local error impl: errors shown fixed position that are wiped when moved from the view
Diffstat (limited to 'packages/web-util/src')
-rw-r--r--packages/web-util/src/hooks/useNotifications.ts98
1 files changed, 96 insertions, 2 deletions
diff --git a/packages/web-util/src/hooks/useNotifications.ts b/packages/web-util/src/hooks/useNotifications.ts
index 8f9e0e835..ca67c5b9b 100644
--- a/packages/web-util/src/hooks/useNotifications.ts
+++ b/packages/web-util/src/hooks/useNotifications.ts
@@ -1,6 +1,6 @@
-import { TranslatedString } from "@gnu-taler/taler-util";
+import { TalerError, TalerErrorCode, TranslatedString } from "@gnu-taler/taler-util";
import { useEffect, useState } from "preact/hooks";
-import { memoryMap } from "../index.browser.js";
+import { memoryMap, useTranslationContext } from "../index.browser.js";
export type NotificationMessage = ErrorNotification | InfoNotification;
@@ -105,3 +105,97 @@ function hash(msg: NotificationMessage): string {
}
return hashCode(str);
}
+
+export function useLocalNotification(): [Notification | undefined, (n: NotificationMessage) => void, (cb: () => Promise<void>) => Promise<void>] {
+ const {i18n} = useTranslationContext();
+
+ const [value, setter] = useState<NotificationMessage>();
+ const notif = !value ? undefined : {
+ message: value,
+ remove: () => {
+ setter(undefined);
+ },
+ }
+
+ async function errorHandling(cb: () => Promise<void>) {
+ try {
+ return await cb()
+ } catch (error: unknown) {
+ if (error instanceof TalerError) {
+ notify(buildRequestErrorMessage(i18n, error))
+ } else {
+ notifyError(
+ i18n.str`Operation failed, please report`,
+ (error instanceof Error
+ ? error.message
+ : JSON.stringify(error)) as TranslatedString
+ )
+ }
+
+ }
+ }
+ return [notif, setter, errorHandling]
+}
+
+type Translator = ReturnType<typeof useTranslationContext>["i18n"]
+
+function buildRequestErrorMessage( i18n: Translator, cause: TalerError): ErrorNotification {
+ let result: ErrorNotification;
+ switch (cause.errorDetail.code) {
+ case TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT: {
+ result = {
+ type: "error",
+ title: i18n.str`Request timeout`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ case TalerErrorCode.WALLET_HTTP_REQUEST_THROTTLED: {
+ result = {
+ type: "error",
+ title: i18n.str`Request throttled`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ case TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE: {
+ result = {
+ type: "error",
+ title: i18n.str`Malformed response`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ case TalerErrorCode.WALLET_NETWORK_ERROR: {
+ result = {
+ type: "error",
+ title: i18n.str`Network error`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ case TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR: {
+ result = {
+ type: "error",
+ title: i18n.str`Unexpected request error`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ default: {
+ result = {
+ type: "error",
+ title: i18n.str`Unexpected error`,
+ description: cause.message as TranslatedString,
+ debug: JSON.stringify(cause.errorDetail, undefined, 2),
+ };
+ break;
+ }
+ }
+ return result;
+}