| | |
| | | * Used to configure the global error handling function, which can monitor vue errors, script errors, static resource errors and Promise errors |
| | | */ |
| | | |
| | | import { errorStore, ErrorInfo } from '/@/store/modules/error'; |
| | | import type { ErrorLogInfo } from '/#/store'; |
| | | |
| | | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; |
| | | |
| | | import { ErrorTypeEnum } from '/@/enums/exceptionEnum'; |
| | | import { App } from 'vue'; |
| | | import projectSetting from '/@/settings/projectSetting'; |
| | |
| | | */ |
| | | |
| | | function vueErrorHandler(err: Error, vm: any, info: string) { |
| | | const errorLogStore = useErrorLogStoreWithOut(); |
| | | const { name, path } = formatComponentName(vm); |
| | | errorStore.commitErrorInfoState({ |
| | | errorLogStore.addErrorLogInfo({ |
| | | type: ErrorTypeEnum.VUE, |
| | | name, |
| | | file: path, |
| | |
| | | if (event === 'Script error.' && !source) { |
| | | return false; |
| | | } |
| | | const errorInfo: Partial<ErrorInfo> = {}; |
| | | const errorInfo: Partial<ErrorLogInfo> = {}; |
| | | colno = colno || (window.event && (window.event as any).errorCharacter) || 0; |
| | | errorInfo.message = event as string; |
| | | if (error?.stack) { |
| | |
| | | errorInfo.stack = ''; |
| | | } |
| | | const name = source ? source.substr(source.lastIndexOf('/') + 1) : 'script'; |
| | | errorStore.commitErrorInfoState({ |
| | | const errorLogStore = useErrorLogStoreWithOut(); |
| | | errorLogStore.addErrorLogInfo({ |
| | | type: ErrorTypeEnum.SCRIPT, |
| | | name: name, |
| | | file: source as string, |
| | | detail: 'lineno' + lineno, |
| | | url: window.location.href, |
| | | ...(errorInfo as Pick<ErrorInfo, 'message' | 'stack'>), |
| | | ...(errorInfo as Pick<ErrorLogInfo, 'message' | 'stack'>), |
| | | }); |
| | | return true; |
| | | } |
| | |
| | | function registerPromiseErrorHandler() { |
| | | window.addEventListener( |
| | | 'unhandledrejection', |
| | | function (event: any) { |
| | | errorStore.commitErrorInfoState({ |
| | | function (event) { |
| | | const errorLogStore = useErrorLogStoreWithOut(); |
| | | errorLogStore.addErrorLogInfo({ |
| | | type: ErrorTypeEnum.PROMISE, |
| | | name: 'Promise Error!', |
| | | file: 'none', |
| | |
| | | 'error', |
| | | function (e: Event) { |
| | | const target = e.target ? e.target : (e.srcElement as any); |
| | | |
| | | errorStore.commitErrorInfoState({ |
| | | const errorLogStore = useErrorLogStoreWithOut(); |
| | | errorLogStore.addErrorLogInfo({ |
| | | type: ErrorTypeEnum.RESOURCE, |
| | | name: 'Resouce Error!', |
| | | name: 'Resource Error!', |
| | | file: (e.target || ({} as any)).currentSrc, |
| | | detail: JSON.stringify({ |
| | | tagName: target.localName, |
| | |
| | | type: e.type, |
| | | }), |
| | | url: window.location.href, |
| | | stack: 'resouce is not found', |
| | | stack: 'resource is not found', |
| | | message: (e.target || ({} as any)).localName + ' is load error', |
| | | }); |
| | | }, |
| | |
| | | */ |
| | | export function setupErrorHandle(app: App) { |
| | | const { useErrorHandle } = projectSetting; |
| | | if (!useErrorHandle) return; |
| | | if (!useErrorHandle) { |
| | | return; |
| | | } |
| | | // Vue exception monitoring; |
| | | app.config.errorHandler = vueErrorHandler; |
| | | |