From 4576da0dd394ee68801b1dc424c9550896d63737 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 25 Jan 2023 15:05:54 +0100 Subject: [PATCH] fix: make sure global error handler handles unrejected promises correctly Resolves https://kolaente.dev/vikunja/frontend/issues/2992 --- src/message/index.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/message/index.ts b/src/message/index.ts index 70902d50b..4e462f75c 100644 --- a/src/message/index.ts +++ b/src/message/index.ts @@ -1,38 +1,40 @@ import {i18n} from '@/i18n' import {notify} from '@kyvg/vue3-notification' -export const getErrorText = (r) => { - - if (r.response && r.response.data) { - if(r.response.data.code) { - const path = `error.${r.response.data.code}` +export function getErrorText(r): string { + let data = undefined + if (r?.response?.data) { + data = r.response.data + } + + if (r?.reason?.response?.data) { + data = r.reason.response.data + } + + if (data) { + if(data.code) { + const path = `error.${data.code}` const message = i18n.global.t(path) // If message and path are equal no translation exists for that error code if (path !== message) { - return [ - r.message, - message, - ] + return message } } - if (r.response.data.message) { - return [ - r.message, - r.response.data.message, - ] + if (data.message) { + return data.message } } - return [r.message] + return r.message } export function error(e, actions = []) { notify({ type: 'error', title: i18n.global.t('error.error'), - text: getErrorText(e), + text: [getErrorText(e)], actions: actions, }) } @@ -41,7 +43,7 @@ export function success(e, actions = []) { notify({ type: 'success', title: i18n.global.t('error.success'), - text: getErrorText(e), + text: [getErrorText(e)], data: { actions: actions, },