How can we listen for errors that do not trigger window.onerror?
window.onerror
is triggered whether it was a syntax or runtime error. This page from quirksmode has lists of what error events it will and will not catch.
Could you please provide a small code example to show how we can listen for such errors? Can we listen for SyntaxError too?
For a small code example to show how we can listen for such errors:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onerror = function (errorMsg, url, lineNumber) {
alert(errorMsg + lineNumber);
// alert("This is a stack trace! Wow! --> %s", error.stack);
};
</script>
</head>
<body>
<script type="text/javascript">
//var x=document.getElementById("demo").value; //uncomment and run to see
document.write('careless to close the parentheses?'; // ')' is not given
</script>
</body>
</html>
running this example in your browser will pop up an alert message similar to this:
JavaScript error: SyntaxError: missing ) after argument list on line 26 for page_url
In the above example: window.onerror = function(message, url, linenumber)
, the arguments are:
message
: the error message (DOMString)url
: the URL of the file containing the error (DOMString)linenumber
: the line number where the error occurred (unsigned long)
If you run the same example by putting var x=document.getElementById("demo").value;
instead of the code with syntax error(as i have shown in the example), it will also be caught by the window.onerror()
function and will show an alert message similar to this:
JavaScript error: TypeError: document.getElementById(...) is null on line 25 for page_url
window.onerror acts something like a global try/catch block, allowing you to gracefully handle(even with server logging) uncaught exceptions you didn’t expect to see:
uncaught exceptions
throw "some messages"
call_something_undefined()
;cross_origin_iframe.contentWindow.document;
, a security exception
some more compile error
<script>{</script>
<script>for(;)</script>
<script>"oops</script>
setTimeout("{", 10);
, it will attempt to compile the first argument as a script
But two major issues described here nicely:
Unlike a local try/catch block, the window.onerror handler doesn’t have direct access to the exception object, and is executed in the global context rather than locally where the error occurred. That means that developers don’t have access to a call stack, and can’t build a call stack themselves by walking up the chain of a method’s callers.
Browsers go to great lengths to sanitize the data provided to the handler in order to prevent unintentional data leakage from cross-origin scripts. If you host your JavaScript on a CDN (as you ought), you’ll get “Script error.”, “”, and 0 in the above handler. That’s not particularly helpful.
shareedit
|
How can we listen for errors that do not trigger window.onerror?的更多相关文章
- Capture and report JavaScript errors with window.onerror
原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html onerror is a sp ...
- A Realistic Evaluation of Memory Hardware Errors and Software System Susceptibility
http://www.cs.rochester.edu/~kshen/papers/usenix2010-li.pdf Abstract Memory hardware reliability is ...
- vconsole h5应用ajax请求抓包
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...
- jQuery JavaScript Library v3.2.1
/*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...
- 未压缩的jQuery
/*! * jQuery JavaScript Library v3.4.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...
- react实战系列 —— React 中的表单和路由的原理
其他章节请看: react实战 系列 React 中的表单和路由的原理 React 中的表单是否简单好用,受控组件和非受控是指什么? React 中的路由原理是什么,如何更好的理解 React 应用的 ...
- ORA 各种oraclesql错误
ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...
- python 常用模块之os、sys、shutil
目录: 1.os 2.sys 3.shutil 一.os模块 说明:os模块是对操作系统进行调用的接口 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 os.chdi ...
- 敏捷项目开源管理软件ScrumBasic(2)- 多项目支持
1.加入Project对象模型 public class Project { [Key] [MaxLength(32)] public string ID { get; set; } public s ...
随机推荐
- DROP CAST - 删除一个用户定义的类型转换
SYNOPSIS DROP CAST (sourcetype AS targettype) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP CAST 删除一个前面 ...
- uva1613 K-Graph Oddity
题目要求k>=最大度数:观察,颜色数量和度数的关系,得颜色数=最大度数+1(偶数)//最大度数(奇数) 可以满足染色关系一个点和周围的点的颜色数加起来最大为它的度数+1: k=所有点中最大的度. ...
- 原生查找DOM的方法
JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...
- 三大框架所使用的UI框架
- render_to_response()
render_to_response('模板名称',字典) 字典:第二个参数必须是为该模板创建context时所使用的字典,如果不提供第二个参数,render_response()使用一个空字典
- github 从一个仓库换到另一个仓库
1.找到.git目录 2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^
- zay大爷的膜你题 D2T1 江城唱晚
依旧是外链... 这一次网易云爆炸了....所以我决定后面的都用QQ 下面是题面 这道题是一道傻逼题 数学题,我们仔细看一看,首先有m朵花的话,我们就有m!种排列方式(也就是m的全排列), 然后我们假 ...
- qcloudsms_py
qcloudsms_py from qcloudsms_py import SmsVoicePromptSender from qcloudsms_py.httpclient import HTTPE ...
- Echarts 异步数据加载遇到的问题
看了Echarts官网异步加载数据的Demo var myChart = echarts.init(document.getElementById('main')); // 显示标题,图例和空的坐标轴 ...
- <Spring Data JPA>二 Spring Data Jpa
1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...