From 3a310ef9db9bca43bcd1ea2510bbc5d6f4fc1f45 Mon Sep 17 00:00:00 2001
From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com>
Date: Thu, 30 Nov 2023 15:36:03 +0700
Subject: [PATCH] feat: error response interceptor
---
.../src/components/GlobalErrorDialog.vue | 39 +++++++++++++++++++
Services/client/src/services/HttpService.ts | 21 ++++++++++
Services/client/src/stores/error.ts | 28 +++++++++++++
3 files changed, 88 insertions(+)
create mode 100644 Services/client/src/components/GlobalErrorDialog.vue
create mode 100644 Services/client/src/stores/error.ts
diff --git a/Services/client/src/components/GlobalErrorDialog.vue b/Services/client/src/components/GlobalErrorDialog.vue
new file mode 100644
index 0000000..a570785
--- /dev/null
+++ b/Services/client/src/components/GlobalErrorDialog.vue
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
+ {{
+ title
+ }}
+
+
+ {{ msg }}
+
+
+
+ (visible = !visible)"
+ />
+
+
+
+
diff --git a/Services/client/src/services/HttpService.ts b/Services/client/src/services/HttpService.ts
index 64bbf6f..7839276 100644
--- a/Services/client/src/services/HttpService.ts
+++ b/Services/client/src/services/HttpService.ts
@@ -1,5 +1,8 @@
import axios from 'axios'
import { getToken } from './KeyCloakService'
+import { useErrorStore } from '@/stores/error'
+
+const error = useErrorStore()
const instance = axios.create()
@@ -8,4 +11,22 @@ instance.interceptors.request.use(async (config) => {
return config
})
+instance.interceptors.response.use(
+ (res) => res,
+ (err) => {
+ const status = err.response.status
+ const data = err.response.data
+
+ error.title = 'เกิดข้อผิดพลาด'
+
+ if (status === 500) {
+ error.msg = 'เกิดข้อผิดพลาด ไม่สามารถดำเนินการต่อได้ กรุณาลองใหม่อีกครั้ง'
+ } else {
+ error.msg = data.message
+ }
+
+ error.show()
+ },
+)
+
export default instance
diff --git a/Services/client/src/stores/error.ts b/Services/client/src/stores/error.ts
new file mode 100644
index 0000000..e69e32a
--- /dev/null
+++ b/Services/client/src/stores/error.ts
@@ -0,0 +1,28 @@
+import { defineStore } from 'pinia'
+import { ref } from 'vue'
+
+export const useErrorStore = defineStore('error-data', () => {
+ const visible = ref(false)
+ const title = ref('')
+ const msg = ref('')
+
+ function set(obj: { title: string; msg: string }, timeout?: number) {
+ title.value = obj.title
+ msg.value = obj.msg
+ show(timeout)
+ }
+
+ function show(timeout: number = -1) {
+ visible.value = true
+
+ if (timeout > 0) {
+ setTimeout(() => (visible.value = false), timeout)
+ }
+ }
+
+ function hide() {
+ visible.value = false
+ }
+
+ return { visible, title, msg, show, hide, set }
+})