前端异常监控window.onerror unhandledrejection
捕获JS异常
window.onerror = function(message, source, lineno, colno, error) { … }
函数参数:
message:错误信息(字符串)。可用于HTML οnerrοr=""处理程序中的event。
source:发生错误的脚本URL(字符串)
lineno:发生错误的行号(数字)
colno:发生错误的列号(数字)
error:Error对象(对象)
若该函数返回true,则阻止执行默认事件处理函数。
window.addEventListener(‘error’, function(event) { … })
window.onerror = function (message, url, line, column, error) {
console.log(‘log—onerror::::’,message, url, line, column, error);
}
window.addEventListener(‘unhandledrejection’, event => {
console.log(event.reason);
});
window.addEventListener(‘unhandledrejection’, event =>
{
console.log(event.reason); // 打印"Unhandle Promise Error!"
});
window.addEventListener(‘rejectionhandled’, event => {
console.log(‘rejection handled’); // 1秒后打印"rejection handled"
});在这里插入代码片