Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Handle http error globally via the axios response interceptor

天舟a83ff154b4f6
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 3def084286..ae8defbe49 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -16,6 +16,7 @@
import { onErrorCaptured } from "vue";
import { useStore } from "vuex";
import ProvideContext from "./components/ProvideContext.vue";
+import { isDev } from "./utils";
export default {
name: "App",
@@ -29,12 +30,16 @@ export default {
});
onErrorCaptured((e: any, _, info) => {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "CRITICAL",
- title: `Internal error occured in '${info}'.`,
- description: e.stack,
- });
+ // If e has response, then we assume it's an http error and has already been
+ // handled by the axios global handler.
+ if (!e.response) {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Internal error occured`,
+ description: isDev() ? e.stack : undefined,
+ });
+ }
return true;
});
},
diff --git a/frontend/src/main.js b/frontend/src/main.js
index f90482c83b..2bae062c59 100644
--- a/frontend/src/main.js
+++ b/frontend/src/main.js
@@ -1,4 +1,5 @@
import { createApp } from "vue";
+import axios from "axios";
import moment from "moment";
import VueClipboard from "vue3-clipboard";
import App from "./App.vue";
@@ -76,6 +77,24 @@ app.config.globalProperties.dataSourceSlug = dataSourceSlug;
registerStoreWithRoleUtil(store);
+axios.interceptors.response.use(
+ (response) => response,
+ (error) => {
+ if (error.response) {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: error.response.data.errors,
+ });
+ throw error;
+ }
+ }
+);
+
+// TODO: A global hook to collect errors to a central service
+// app.config.errorHandler = function (err, vm, info) {
+// };
+
app
// Need to use a directive on the element.
// The normal hljs.initHighlightingOnLoad() won't work because router change would cause vue