原文: http://stackoverflow.com/questions/19141195/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:

  1. message: the error message (DOMString)
  2. url: the URL of the file containing the error (DOMString)
  3. 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

    1. throw "some messages"
    2. call_something_undefined();
    3. cross_origin_iframe.contentWindow.document;, a security exception
  • some more compile error

    1. <script>{</script>
    2. <script>for(;)</script>
    3. <script>"oops</script>
    4. setTimeout("{", 10);, it will attempt to compile the first argument as a script

But two major issues described here nicely:

  1. 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.

  2. 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
answered Oct 2 '13 at 16:03
Sage

12k11629

How can we listen for errors that do not trigger window.onerror?的更多相关文章

  1. 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 ...

  2. 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 ...

  3. vconsole h5应用ajax请求抓包

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...

  4. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  5. 未压缩的jQuery

    /*! * jQuery JavaScript Library v3.4.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  6. react实战系列 —— React 中的表单和路由的原理

    其他章节请看: react实战 系列 React 中的表单和路由的原理 React 中的表单是否简单好用,受控组件和非受控是指什么? React 中的路由原理是什么,如何更好的理解 React 应用的 ...

  7. ORA 各种oraclesql错误

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  8. python 常用模块之os、sys、shutil

    目录: 1.os 2.sys 3.shutil 一.os模块 说明:os模块是对操作系统进行调用的接口 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 os.chdi ...

  9. 敏捷项目开源管理软件ScrumBasic(2)- 多项目支持

    1.加入Project对象模型 public class Project { [Key] [MaxLength(32)] public string ID { get; set; } public s ...

随机推荐

  1. VsCode使用之HTML 中 CSS Class 智能提示

    HTML 中 CSS Class 智能提示 安装插件:HTML CSS Support 设置中添加以下代码: "editor.parameterHints": true, &quo ...

  2. python基础一 day5 集合

    集合是无序的 增:add()添加进去是无序,不一定是最后面,update()像extend() 删: 没有改,有查,里面的元素是不可变类型 查用for in 交集: 并集: 反交集 叉集: 子集与超集 ...

  3. delphi中使用自定义资源的方法

    如果要在delphi中使用自定义资源文件*.res文件,比如一个光标,此时可以采用下列步骤: 1,创建包含相应的资源文件,这里是创建一个包含自定义光标的res文件. 2,在主窗体的pas文件中加入编译 ...

  4. mybatis 实现批量更新

    更新单条记录 1 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 1 UPDATE course SET  ...

  5. QT+模态对话框与非模态对话框

    #include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAct ...

  6. Hibernate中tx.commit()

    hibernate.cfg,xml文件中的自动提交事务是false.主键生成策略是native. 在表的映射继承是手动提交事务(即:tx.commit())无法发出sql语句,把数据插入到数据库的表中 ...

  7. MySQL-03 SQL语句设计

    学习要点 SQL语句分类 DML语句 DML 查询语句 SQL语句分类 数据操纵语言(DML):用来操纵数据库中数据的命令.包括:SELECT.INSERT.UPDATE.DELETE. 数据定义语言 ...

  8. docker 私有镜像服务器搭建

    1.准备一台服务器A(已安装docker, IP:192.168.39.111) 2.在服务器A上通过运行registry容器进行搭建 docker run -itd -v /my_registry: ...

  9. 05 selenium模块

    selenium模块 selenium selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. selenium最初是一个自动化测试工具,而爬虫中使 ...

  10. Apollo配置中心的使用

    1. 自己搭建Apollo配置中心 碰到如下错误: nested exception is org.hibernate.HibernateException: Access to DialectRes ...