We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
目前团队基建已经进行到异常监控这个环节,需要系统性的了解前端异常。
Javascript
Ajax
Iframe
Script error
try/catch只能捕获到同步的运行时异常,对语法和异步错误捕获不到。
try/catch
1、语法错误在开发阶段就可以避免 2、对于异步的错误。Promise可以通过unhandledrejection全局捕获。setTimeout可以使用window.onerror捕获
window.onerror不是万能的,它不能捕获Promise异常、语法错误、静态资源异常和接口异常。也可以捕获iframe异常
iframe
<iframe src="./iframe.html" frameborder="0"></iframe> <script> window.frames[0].onerror = function (message, source, lineno, colno, error) { console.log('捕获到 iframe 异常:',{message, source, lineno, colno, error}); return true; }; </script>
用于处理静态资源加载异常
需要注意的是,window.onerror 函数只有在返回 true 的时候,异常才不会向上抛出,否则即使是知道异常的发生控制台还是会显示 Uncaught Error: xxxxx
window.onerror
true
Uncaught Error: xxxxx
用于处理未捕获的promise异常
用于统一处理使用catch捕获的promise异常
catch
React16提供error boundaries。但是会存在几种异常捕获不到。
error boundaries
The text was updated successfully, but these errors were encountered:
No branches or pull requests
为什么需要处理前端异常?
需要处理哪些异常?
Javascript
语法错误,代码异常Ajax
请求异常(前端针对ajax异常进行相应的页面处理)Iframe
异常Script error
跨域异常(cross-Origin解决)异常如何处理
try/catch
window.onerror
window.addEventListener('error')
需要注意的是,
window.onerror
函数只有在返回true
的时候,异常才不会向上抛出,否则即使是知道异常的发生控制台还是会显示Uncaught Error: xxxxx
window.addEventListener('unhandledrejection')
window.addEventListener('rejectionhandled')
React异常捕获
error boundaries
区域内的代码参考
The text was updated successfully, but these errors were encountered: