Browse Source

添加自定义异常

DESKTOP-MINPJAU\Administrator 3 năm trước cách đây
mục cha
commit
78001b757b
1 tập tin đã thay đổi với 41 bổ sung0 xóa
  1. 41 0
      src/utils/BizException.ts

+ 41 - 0
src/utils/BizException.ts

@@ -0,0 +1,41 @@
+// @ts-ignore
+import {ElMessage, ElMessageBox} from "element-plus";
+
+export const ExceptionEnum = {
+    MESSAGE_ERROR: {code: 1001, name: '错误', turnOnHtml: false},
+    MESSAGE_HTML_ERROR: {code: 1002, name: '错误', turnOnHtml: true},
+
+    LOGICAL_ERROR: {code: 2001, name: '成功', turnOnHtml: false},
+    LOGICAL_HTML_ERROR: {code: 2002, name: '成功', turnOnHtml: true},
+
+} as const;
+
+class ExceptionCode {
+    code: number;
+    name: string;
+    turnOnHtml: boolean;
+}
+
+export const BizException = (code: ExceptionCode, message?: string) => {
+    if (code.code > 1000 && code.code < 2000) {
+        ElMessage({
+            type: 'error',
+            message: message,
+            dangerouslyUseHTMLString: code.turnOnHtml,
+            duration: 2500,
+            showClose: true
+        })
+    } else if (code.code > 2000 && code.code < 3000) {
+        ElMessageBox.alert(message, '提示', {
+            type: 'error',
+            dangerouslyUseHTMLString: code.turnOnHtml,
+        }).then(() => {
+
+        }).catch(() => {
+
+        })
+    }
+    throw new Error(message)
+}
+
+