From e416fbabfffd50597e77b7edf4a851a01a8b705a Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Thu, 5 Nov 2020 16:39:59 -0500 Subject: [PATCH] Add unexpected error util --- app/src/lang/en-US/index.json | 1 + app/src/utils/notify.ts | 6 ++++-- app/src/utils/unexpected-error.ts | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 app/src/utils/unexpected-error.ts diff --git a/app/src/lang/en-US/index.json b/app/src/lang/en-US/index.json index 87af6e3fde..80e0888924 100644 --- a/app/src/lang/en-US/index.json +++ b/app/src/lang/en-US/index.json @@ -612,6 +612,7 @@ "ITEM_LIMIT_REACHED": "Item limit reached.", "ITEM_NOT_FOUND": "Item not found.", "ROUTE_NOT_FOUND": "Not found.", + "UNKNOWN": "Unexpected Error", "-1": "Couldn't Reach API" }, diff --git a/app/src/utils/notify.ts b/app/src/utils/notify.ts index 7f6b432e82..0405d56cee 100644 --- a/app/src/utils/notify.ts +++ b/app/src/utils/notify.ts @@ -1,7 +1,9 @@ import { useNotificationsStore } from '@/stores/'; import { NotificationRaw } from '@/types'; +let store: any; + export function notify(notification: NotificationRaw) { - const notificationsStore = useNotificationsStore(); - notificationsStore.add(notification); + if (!store) store = useNotificationsStore(); + store.add(notification); } diff --git a/app/src/utils/unexpected-error.ts b/app/src/utils/unexpected-error.ts new file mode 100644 index 0000000000..92dc9a3333 --- /dev/null +++ b/app/src/utils/unexpected-error.ts @@ -0,0 +1,19 @@ +import { useNotificationsStore } from '@/stores/'; +import { i18n } from '@/lang'; +import { RequestError } from '@/api'; + +let store: any; + +export function unexpectedError(error: Error | RequestError) { + if (!store) store = useNotificationsStore(); + + const code = (error as RequestError).response?.data?.errors?.[0]?.extensions?.code || 'UNKNOWN'; + const message = (error as RequestError).response?.data?.errors?.[0]?.message || error.message || undefined; + + store.add({ + title: i18n.t(`errors.${code}`), + text: message, + type: 'error', + dialog: true, + }); +}