From b1e697b3f0192faa9555cbbec69b0b98f0b4296a Mon Sep 17 00:00:00 2001 From: Saeid Date: Sat, 7 Sep 2019 10:24:41 +0430 Subject: [PATCH] error handler --- public/mix-manifest.json | 10 ++--- resources/js/Global/plugins/auth.js | 4 +- resources/js/Global/services/api.services.js | 1 - .../Global/services/errorHandler.services.js | 37 +++++++++++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 resources/js/Global/services/errorHandler.services.js diff --git a/public/mix-manifest.json b/public/mix-manifest.json index e59dd88..1258f20 100644 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -1,7 +1,7 @@ { - "/js/vue/Authentication/app.js": "/js/vue/Authentication/app.js?id=c9ebdffec50d82145867", - "/js/vue/Home/app.js": "/js/vue/Home/app.js?id=e2cd567ef427a1285194", - "/js/vue/Modules/CRM/app.js": "/js/vue/Modules/CRM/app.js?id=21ca2c0fd50704c54125", - "/js/vue/Modules/Reservation/app.js": "/js/vue/Modules/Reservation/app.js?id=ecddb140ee87bb6dfc7c", - "/js/vue/User/app.js": "/js/vue/User/app.js?id=ba20da8154c5c04c0dd6" + "/js/vue/Authentication/app.js": "/js/vue/Authentication/app.js?id=e69040a72a11b09df6af", + "/js/vue/Home/app.js": "/js/vue/Home/app.js?id=c900c418614124564483", + "/js/vue/Modules/CRM/app.js": "/js/vue/Modules/CRM/app.js?id=2790386807f21cd4157c", + "/js/vue/Modules/Reservation/app.js": "/js/vue/Modules/Reservation/app.js?id=0741c42d460602f07f70", + "/js/vue/User/app.js": "/js/vue/User/app.js?id=f06263f329a1eec8ecec" } diff --git a/resources/js/Global/plugins/auth.js b/resources/js/Global/plugins/auth.js index 534555e..ff79e38 100644 --- a/resources/js/Global/plugins/auth.js +++ b/resources/js/Global/plugins/auth.js @@ -15,4 +15,6 @@ if (PermissionService.get()) { if (UserService.get()) { store.commit('auth/SET_AUTH_USER', UserService.get()) -} \ No newline at end of file +} + +import '@Global/services/errorHandler.services' \ No newline at end of file diff --git a/resources/js/Global/services/api.services.js b/resources/js/Global/services/api.services.js index 120c465..9015192 100644 --- a/resources/js/Global/services/api.services.js +++ b/resources/js/Global/services/api.services.js @@ -5,7 +5,6 @@ const ApiService = { init(baseURL = null) { if (baseURL) { - // baseURL = 'https://www.willaengine.ir' axios.defaults.baseURL = baseURL; } diff --git a/resources/js/Global/services/errorHandler.services.js b/resources/js/Global/services/errorHandler.services.js new file mode 100644 index 0000000..24be7b3 --- /dev/null +++ b/resources/js/Global/services/errorHandler.services.js @@ -0,0 +1,37 @@ +import axios from "axios"; +import { TokenService } from "@Global/services/storage.services"; +import ApiService from "@Global/services/api.services"; +import store from "@Core/store/index"; +function errorResponseHandler(error) { + + // check for errorHandle config + if ( + error.config.hasOwnProperty("errorHandle") && + error.config.errorHandle === false + ) { + return Promise.reject(error); + } + + // if has response show the error + if (error.response) { + if(error.response.status == 401) { + TokenService.removeToken(); + ApiService.removeAuthHeader(); + store.commit('auth/logout', {}, {root: true}) + window.location.href = '/Login'; + } + } +} +function successHandler(response) { + // if(response.status == 200) { + // if(typeof response.data.message !== 'undefined') { + // toast.success(response.data.message, response.data.title); + // } + // } + return response; +} + +// apply interceptor on response +axios.interceptors.response.use(successHandler, errorResponseHandler); + +export default errorResponseHandler; \ No newline at end of file