From 9780fe596fa0af4212f308a3150d1066a3275517 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 21 Oct 2024 13:33:12 +0200 Subject: [PATCH] feat: refs #8039 notify error unify --- src/boot/axios.js | 34 +--------------------------------- src/boot/quasar.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/boot/axios.js b/src/boot/axios.js index d3e981adb..b084b835d 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -40,37 +40,7 @@ const onResponse = (response) => { const onResponseError = (error) => { stateQuery.remove(error.config); - let message = ''; - - const response = error.response; - const responseData = response?.data; - const responseError = responseData && response.data.error; - if (responseError) { - message = responseError.message; - } - - switch (response?.status) { - case 422: - if (error.name == 'ValidationError') - message += - ' "' + - responseError.details.context + - '.' + - Object.keys(responseError.details.codes).join(',') + - '"'; - break; - case 500: - message = 'errors.statusInternalServerError'; - break; - case 502: - message = 'errors.statusBadGateway'; - break; - case 504: - message = 'errors.statusGatewayTimeout'; - break; - } - - if (session.isLoggedIn() && response?.status === 401) { + if (session.isLoggedIn() && error.response?.status === 401) { session.destroy(false); const hash = window.location.hash; const url = hash.slice(1); @@ -79,8 +49,6 @@ const onResponseError = (error) => { return Promise.reject(error); } - if (!(error instanceof CanceledError)) notify(message, 'negative'); - return Promise.reject(error); }; diff --git a/src/boot/quasar.js b/src/boot/quasar.js index 41a7990c7..bf5175ee6 100644 --- a/src/boot/quasar.js +++ b/src/boot/quasar.js @@ -10,8 +10,39 @@ export default boot(({ app }) => { app.mixin(qFormMixin); app.mixin(mainShortcutMixin); app.directive('shortcut', keyShortcut); - app.config.errorHandler = function (err) { - console.error(err); - if (!(err instanceof CanceledError)) notify('globals.error', 'negative', 'error'); + app.config.errorHandler = (error) => { + let message; + const response = error.response; + const responseData = response?.data; + const responseError = responseData && response.data.error; + if (responseError) { + message = responseError.message; + } + + switch (response?.status) { + case 422: + if (error.name == 'ValidationError') + message += + ' "' + + responseError.details.context + + '.' + + Object.keys(responseError.details.codes).join(',') + + '"'; + break; + case 500: + message = 'errors.statusInternalServerError'; + break; + case 502: + message = 'errors.statusBadGateway'; + break; + case 504: + message = 'errors.statusGatewayTimeout'; + break; + } + + console.error(error); + if (error instanceof CanceledError) return; + + notify(message ?? 'globals.error', 'negative', 'error'); }; });