BizException.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {ElMessage, ElMessageBox} from "element-plus";
  2. export const ExceptionEnum = {
  3. MESSAGE_ERROR: {code: 1001, name: '错误', turnOnHtml: false},
  4. MESSAGE_HTML_ERROR: {code: 1002, name: '错误', turnOnHtml: true},
  5. LOGICAL_ERROR: {code: 2001, name: '成功', turnOnHtml: false},
  6. LOGICAL_HTML_ERROR: {code: 2002, name: '成功', turnOnHtml: true},
  7. };
  8. class ExceptionCode {
  9. code: number;
  10. name: string;
  11. turnOnHtml: boolean;
  12. }
  13. export const BizException = (code: ExceptionCode, message?: string) => {
  14. if (code.code > 1000 && code.code < 2000) {
  15. ElMessage({
  16. type: 'error',
  17. message: message,
  18. dangerouslyUseHTMLString: code.turnOnHtml,
  19. duration: 2500,
  20. showClose: true,
  21. grouping: true,
  22. })
  23. } else if (code.code > 2000 && code.code < 3000) {
  24. ElMessageBox.alert(message, '提示', {
  25. type: 'error',
  26. dangerouslyUseHTMLString: code.turnOnHtml,
  27. }).then(() => {
  28. }).catch(() => {
  29. })
  30. }
  31. throw new Error(message)
  32. }