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 ...
随机推荐
- dropuser - 删除一个 PostgreSQL 用户帐户
SYNOPSIS dropuser [ option...] [ username] DESCRIPTION 描述 dropuser 删除一个现有 PostgreSQL 用户 和 该用户所有的数据库. ...
- vue >>> 编译失败问题 loader 待解决( iview vue脚手架生成)
vue >>> 编译失败问题 loader 待解决 用vue iview 脚手架 来一次试试~
- docker 镜像管理操作
镜像特点 1. 分层存储的文件 2.一个软件运行环境 3.一个镜像可以创建多个容器 4.一种标准交付 5.不包含Linux内核而又精简的Linux操作系统 6.不是一个单一的文件而是由多层构成的,可以 ...
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- TUM好用的工具
https://vision.in.tum.de/data/datasets/rgbd-dataset/tools?tdsourcetag=s_pctim_aiomsg
- 开源代码生成器,基于mybatis-generator扩展,结合freemarker
git源码地址:https://github.com/JonSnow592622272/free-generator-code 码云gitee源码地址:https://gitee.com/a59262 ...
- activiti工作流学习链接
首页: http://www.activiti.org/书籍: activiti in action 入门demo: kft-activiti-demo http://www.oschina.n ...
- 重载操作符 'operator'
operator 是 C++ 的(运算符的)重载操作符.用作扩展运算符的功能. 它和运算符一起使用,表示一个运算符函数,理解时应将 [operator+运算符] 整体上视为一个函数名. 要注意的是: ...
- 如何用纯 CSS 创作一个渐变色动画边框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odpRKX 可交互视频教 ...
- tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中 (转)
http://blog.chinaunix.net/uid-30025978-id-4788683.html 1.首先还是要将u-boot写入SD卡中从SD卡启动. 使用读卡器将SD插入电脑中,使用u ...
