[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]try { [size=1.4] let name = 'jartto'; [size=1.4] console.log(nam); [size=1.4]} catch(e) { [size=1.4] console.log('捕获到异常:',e); [size=1.4]} |
[size=1.4]捕获到异常: ReferenceError: nam is not defined [size=1.4] at <anonymous>:3:15 |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 | [size=1.4]try { [size=1.4] let name = 'jartto; [size=1.4] console.log(nam); [size=1.4]} catch(e) { [size=1.4] [size=1.4] console.log('捕获到异常:',e); [size=1.4]} |
[size=1.4]Uncaught SyntaxError: Invalid or unexpected token |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 | [size=1.4]try { [size=1.4] setTimeout(() => { [size=1.4] undefined.map(v => v); [size=1.4] }, 1000) [size=1.4]} catch(e) { [size=1.4] console.log('捕获到异常:',e); [size=1.4]} |
[size=1.4]Uncaught TypeError: Cannot read property 'map' of undefined [size=1.4] at setTimeout (<anonymous>:3:11) |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 [size=1.4]9 [size=1.4]10 [size=1.4]11 | [size=1.4]/** [size=1.4]* @param {String} message 错误信息 [size=1.4]* @param {String} source 出错文件 [size=1.4]* @param {Number} lineno 行号 [size=1.4]* @param {Number} colno 列号 [size=1.4]* @param {Object} error Error对象(对象) [size=1.4]*/ [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4] console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4]} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 [size=1.4]9 | [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4]// message:错误信息(字符串)。 [size=1.4]// source:发生错误的脚本URL(字符串) [size=1.4]// lineno:发生错误的行号(数字) [size=1.4]// colno:发生错误的列号(数字) [size=1.4]// error:Error对象(对象) [size=1.4]console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4]} [size=1.4]Jartto; |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 | [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4]console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4]} [size=1.4]let name = 'Jartto |
[size=1.4]Uncaught SyntaxError: Invalid or unexpected token |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4] console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4]} [size=1.4]setTimeout(() => { [size=1.4] Jartto; [size=1.4]}); |
[size=1.4]捕获到异常: {message: "Uncaught ReferenceError: Jartto is not defined", source: "http://127.0.0.1:8001/", lineno: 36, colno: 5, error: ReferenceError: Jartto is not defined [size=1.4] at setTimeout (http://127.0.0.1:8001/:36:5)} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 | [size=1.4]<script> [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4] console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4] return true; [size=1.4]} [size=1.4]</script> [size=1.4]<img src="./jartto.png"> |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 | [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4] console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4] return true; [size=1.4]} [size=1.4]setTimeout(() => { [size=1.4] Jartto; [size=1.4]}); |
[size=1.4]Uncaught ReferenceError: Jartto is not defined [size=1.4] at setTimeout ((index):36) |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]<scritp> [size=1.4]window.addEventListener('error', (error) => { [size=1.4] console.log('捕获到异常:', error); [size=1.4]}, true) [size=1.4]</script> [size=1.4]<img src="./jartto.png"> |
[size=1.4]1 [size=1.4]2 [size=1.4]3 | [size=1.4]window.addEventListener("unhandledrejection", function(e){ [size=1.4] console.log(e); [size=1.4]}); |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]window.addEventListener("unhandledrejection", function(e){ [size=1.4] e.preventDefault() [size=1.4] console.log('捕获到异常:', e); [size=1.4] return true; [size=1.4]}); [size=1.4]Promise.reject('promise error'); |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 | [size=1.4]window.addEventListener("unhandledrejection", function(e){ [size=1.4] e.preventDefault() [size=1.4] console.log('捕获到异常:', e); [size=1.4] return true; [size=1.4]}); [size=1.4]new Promise((resolve, reject) => { [size=1.4] reject('jartto: promise error'); [size=1.4]}); |
[size=1.4]event.preventDefault(); |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]Vue.config.errorHandler = (err, vm, info) => { [size=1.4] console.error('通过vue errorHandler捕获的错误'); [size=1.4] console.error(err); [size=1.4] console.error(vm); [size=1.4] console.error(info); [size=1.4]} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 | [size=1.4]componentDidCatch(error, info) { [size=1.4] console.log(error, info); [size=1.4]} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 [size=1.4]9 [size=1.4]10 [size=1.4]11 [size=1.4]12 [size=1.4]13 [size=1.4]14 [size=1.4]15 [size=1.4]16 [size=1.4]17 [size=1.4]18 [size=1.4]19 [size=1.4]20 [size=1.4]21 | [size=1.4]class ErrorBoundary extends React.Component { [size=1.4] constructor(props) { [size=1.4] super(props); [size=1.4] this.state = { hasError: false }; [size=1.4] } [size=1.4] componentDidCatch(error, info) { [size=1.4] // Display fallback UI [size=1.4] this.setState({ hasError: true }); [size=1.4] // You can also log the error to an error reporting service [size=1.4] logErrorToMyService(error, info); [size=1.4] } [size=1.4] render() { [size=1.4] if (this.state.hasError) { [size=1.4] // You can render any custom fallback UI [size=1.4] return <h1>Something went wrong.</h1>; [size=1.4] } [size=1.4] return this.props.children; [size=1.4] } [size=1.4]} |
[size=1.4]<ErrorBoundary> [size=1.4] <MyWidget /> [size=1.4]</ErrorBoundary> |
[size=1.4]1 [size=1.4]2 [size=1.4]3 | [size=1.4]window.onerror = function(message, source, lineno, colno, error) { [size=1.4] console.log('捕获到异常:',{message, source, lineno, colno, error}); [size=1.4]} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 | [size=1.4]<iframe src="./iframe.html" frameborder="0"></iframe> [size=1.4]<script> [size=1.4] window.frames[0].onerror = function (message, source, lineno, colno, error) { [size=1.4] console.log('捕获到 iframe 异常:',{message, source, lineno, colno, error}); [size=1.4] return true; [size=1.4] }; [size=1.4]</script> |
[size=1.4]1 | [size=1.4]<script src="http://jartto.wang/main.js" crossorigin></script> |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 | [size=1.4]const script = document.createElement('script'); [size=1.4]script.crossOrigin = 'anonymous'; [size=1.4]script.src = url; [size=1.4]document.body.appendChild(script); |
[size=1.4]const originAddEventListener = EventTarget.prototype.addEventListener; [size=1.4]EventTarget.prototype.addEventListener = function (type, listener, options) { [size=1.4] const wrappedListener = function (...args) { [size=1.4] try { [size=1.4] return listener.apply(this, args); [size=1.4] } [size=1.4] catch (err) { [size=1.4] throw err; [size=1.4] } [size=1.4] } [size=1.4] return originAddEventListener.call(this, type, wrappedListener, options); [size=1.4]} |
[size=1.4](() => { [size=1.4] const originAddEventListener = EventTarget.prototype.addEventListener; [size=1.4] EventTarget.prototype.addEventListener = function (type, listener, options) { [size=1.4]+ // 捕获添加事件时的堆栈 [size=1.4]+ const addStack = new Error(`Event (${type})`).stack; [size=1.4] const wrappedListener = function (...args) { [size=1.4] try { [size=1.4] return listener.apply(this, args); [size=1.4] } [size=1.4] catch (err) { [size=1.4]+ // 异常发生时,扩展堆栈 [size=1.4]+ err.stack += '\n' + addStack; [size=1.4] throw err; [size=1.4] } [size=1.4] } [size=1.4] return originAddEventListener.call(this, type, wrappedListener, options); [size=1.4] } [size=1.4] })(); |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 [size=1.4]7 [size=1.4]8 [size=1.4]9 [size=1.4]10 [size=1.4]11 [size=1.4]12 [size=1.4]13 [size=1.4]14 [size=1.4]15 [size=1.4]16 [size=1.4]17 [size=1.4]18 | [size=1.4]window.addEventListener('load', function () { [size=1.4] sessionStorage.setItem('good_exit', 'pending'); [size=1.4] setInterval(function () { [size=1.4] sessionStorage.setItem('time_before_crash', new Date().toString()); [size=1.4] }, 1000); [size=1.4] }); [size=1.4] window.addEventListener('beforeunload', function () { [size=1.4] sessionStorage.setItem('good_exit', 'true'); [size=1.4] }); [size=1.4] if(sessionStorage.getItem('good_exit') && [size=1.4] sessionStorage.getItem('good_exit') !== 'true') { [size=1.4] /* [size=1.4] insert crash logging code here [size=1.4] */ [size=1.4] alert('Hey, welcome back from your crash, looks like you crashed on: ' + sessionStorage.getItem('time_before_crash')); [size=1.4] } |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 | [size=1.4]function report(error) { [size=1.4] let reportUrl = 'http://jartto.wang/report'; [size=1.4] new Image().src = `${reportUrl}?logs=${error}`; [size=1.4]} |
[size=1.4]1 [size=1.4]2 [size=1.4]3 [size=1.4]4 [size=1.4]5 [size=1.4]6 | [size=1.4]Reporter.send = function(data) { [size=1.4] // 只采集 30% [size=1.4] if(Math.random() < 0.3) { [size=1.4] send(data) // 上报错误信息 [size=1.4] } [size=1.4]} |
文章转载自 Jartto's blog
链接:http://jartto.wang/2018/11/20/js-exception-handling
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |