vue-vben-admin/src/setup/error-handle/index.ts

178 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-11-18 22:41:59 +08:00
/**
* Used to configure the global error handling function, which can monitor vue errors, script errors, static resource errors and Promise errors
*/
2020-10-18 21:55:21 +08:00
import { errorStore, ErrorInfo } from '/@/store/modules/error';
import { useSetting } from '/@/hooks/core/useSetting';
import { ErrorTypeEnum } from '/@/enums/exceptionEnum';
import { App } from 'vue';
2020-11-18 22:41:59 +08:00
/**
* Handling error stack information
* @param error
*/
2020-10-18 21:55:21 +08:00
function processStackMsg(error: Error) {
if (!error.stack) {
return '';
}
let stack = error.stack
2020-11-18 22:41:59 +08:00
.replace(/\n/gi, '') // Remove line breaks to save the size of the transmitted content
.replace(/\bat\b/gi, '@') // At in chrome, @ in ff
.split('@') // Split information with @
.slice(0, 9) // The maximum stack length (Error.stackTraceLimit = 10), so only take the first 10
.map((v) => v.replace(/^\s*|\s*$/g, '')) // Remove extra spaces
.join('~') // Manually add separators for later display
.replace(/\?[^:]+/gi, ''); // Remove redundant parameters of js file links (?x=1 and the like)
2020-10-18 21:55:21 +08:00
const msg = error.toString();
if (stack.indexOf(msg) < 0) {
stack = msg + '@' + stack;
}
return stack;
}
2020-11-18 22:41:59 +08:00
/**
* get comp name
* @param vm
*/
2020-10-18 21:55:21 +08:00
function formatComponentName(vm: any) {
if (vm.$root === vm) {
return {
name: 'root',
path: 'root',
};
}
const options = vm.$options as any;
if (!options) {
return {
name: 'anonymous',
path: 'anonymous',
};
}
const name = options.name || options._componentTag;
return {
name: name,
path: options.__file,
};
}
2020-11-18 22:41:59 +08:00
/**
* Configure Vue error handling function
*/
2020-10-18 21:55:21 +08:00
function vueErrorHandler(err: Error, vm: any, info: string) {
const { name, path } = formatComponentName(vm);
errorStore.commitErrorInfoState({
type: ErrorTypeEnum.VUE,
name,
file: path,
message: err.message,
stack: processStackMsg(err),
detail: info,
url: window.location.href,
});
}
2020-11-18 22:41:59 +08:00
/**
* Configure script error handling function
*/
2020-10-18 21:55:21 +08:00
export function scriptErrorHandler(
event: Event | string,
source?: string,
lineno?: number,
colno?: number,
error?: Error
) {
if (event === 'Script error.' && !source) {
return false;
}
2020-10-30 21:32:05 +08:00
const errorInfo: Partial<ErrorInfo> = {};
colno = colno || (window.event && (window.event as any).errorCharacter) || 0;
errorInfo.message = event as string;
if (error && error.stack) {
errorInfo.stack = error.stack;
} else {
errorInfo.stack = '';
}
const name = source ? source.substr(source.lastIndexOf('/') + 1) : 'script';
errorStore.commitErrorInfoState({
type: ErrorTypeEnum.SCRIPT,
name: name,
file: source as string,
detail: 'lineno' + lineno,
url: window.location.href,
...(errorInfo as Pick<ErrorInfo, 'message' | 'stack'>),
});
2020-10-18 21:55:21 +08:00
return true;
}
2020-11-18 22:41:59 +08:00
/**
* Configure Promise error handling function
*/
2020-10-18 21:55:21 +08:00
function registerPromiseErrorHandler() {
window.addEventListener(
'unhandledrejection',
function (event: any) {
errorStore.commitErrorInfoState({
type: ErrorTypeEnum.PROMISE,
name: 'Promise Error!',
file: 'none',
detail: 'promise error!',
url: window.location.href,
stack: 'promise error!',
message: event.reason,
});
},
true
);
}
2020-11-18 22:41:59 +08:00
/**
* Configure monitoring resource loading error handling function
*/
2020-10-18 21:55:21 +08:00
function registerResourceErrorHandler() {
2020-11-18 22:41:59 +08:00
// Monitoring resource loading error(img,script,css,and jsonp)
2020-10-18 21:55:21 +08:00
window.addEventListener(
'error',
function (e: Event) {
const target = e.target ? e.target : (e.srcElement as any);
errorStore.commitErrorInfoState({
type: ErrorTypeEnum.RESOURCE,
name: 'Resouce Error!',
file: (e.target || ({} as any)).currentSrc,
detail: JSON.stringify({
tagName: target.localName,
html: target.outerHTML,
type: e.type,
}),
url: window.location.href,
stack: 'resouce is not found',
message: (e.target || ({} as any)).localName + ' is load error',
});
},
true
);
}
2020-11-18 22:41:59 +08:00
/**
* Configure global error handling
* @param app
*/
2020-10-18 21:55:21 +08:00
export function setupErrorHandle(app: App) {
const { projectSetting } = useSetting();
const { useErrorHandle } = projectSetting;
2020-11-18 22:41:59 +08:00
if (!useErrorHandle) return;
// Vue exception monitoring;
2020-10-18 21:55:21 +08:00
app.config.errorHandler = vueErrorHandler;
2020-11-18 22:41:59 +08:00
// script error
2020-10-18 21:55:21 +08:00
window.onerror = scriptErrorHandler;
2020-11-18 22:41:59 +08:00
// promise exception
2020-10-18 21:55:21 +08:00
registerPromiseErrorHandler();
2020-11-18 22:41:59 +08:00
// Static resource exception
2020-10-18 21:55:21 +08:00
registerResourceErrorHandler();
}