From 1c7610ad08a3cc4099c0f6bc04560c306354dfe7 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Tue, 25 Sep 2018 08:16:52 +0200 Subject: [PATCH] Improved JS error handling --- frontend/src/common/api.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/frontend/src/common/api.js b/frontend/src/common/api.js index 037a05151..d9615991f 100644 --- a/frontend/src/common/api.js +++ b/frontend/src/common/api.js @@ -27,12 +27,19 @@ Api.interceptors.response.use(function (response) { console.log(error); } - const data = error.response.data; + let errorMessage = 'An error occurred - are you offline?'; + let code = error.code; + + if(error.response && error.response.data) { + let data = error.response.data; + code = data.code; + errorMessage = data.message ? data.message : data.error; + } Event.publish('ajax.end'); - Event.publish('alert.error', data.message ? data.message : data.error); + Event.publish('alert.error', errorMessage); - if(data.code === 401) { + if(code === 401) { window.location = '/'; }