diff --git a/src/boot/axios.js b/src/boot/axios.js index 1f384ff3..66b2ffe9 100644 --- a/src/boot/axios.js +++ b/src/boot/axios.js @@ -2,7 +2,9 @@ import { boot } from 'quasar/wrappers'; import { Connection } from '../js/db/connection'; import { userStore } from 'stores/user'; import axios from 'axios'; +import useNotify from 'src/composables/useNotify.js'; +const { notify } = useNotify(); // Be careful when using SSR for cross-request state pollution // due to creating a Singleton instance here; // If any client changes this (global) instance, it might be a @@ -12,9 +14,27 @@ import axios from 'axios'; const api = axios.create({ baseURL: `//${location.hostname}:${location.port}/api/` }); - const jApi = new Connection(); +const onRequestError = error => { + return Promise.reject(error); +}; + +const onResponseError = error => { + let message = ''; + + const response = error.response; + const responseData = response && response.data; + const responseError = responseData && response.data.error; + if (responseError) { + message = responseError.message; + } + + notify(message, 'negative'); + + return Promise.reject(error); +}; + export default boot(({ app }) => { const user = userStore(); function addToken(config) { @@ -23,7 +43,9 @@ export default boot(({ app }) => { } return config; } - api.interceptors.request.use(addToken); + api.interceptors.request.use(addToken, onRequestError); + api.interceptors.response.use(response => response, onResponseError); + jApi.use(addToken); // for use inside Vue files (Options API) through this.$axios and this.$api diff --git a/src/components/common/VnForm.vue b/src/components/common/VnForm.vue index e4ff648c..450f7000 100644 --- a/src/components/common/VnForm.vue +++ b/src/components/common/VnForm.vue @@ -1,5 +1,5 @@