Capture and report JavaScript errors with window.onerror
原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html
onerror is a special browser event that fires whenever an uncaught JavaScript error has been thrown. It’s one of the easiest ways to log client-side errors and report them to your servers. It’s also one of the major mechanisms by which Sentry’s client JavaScript integration (raven-js) works.
You listen to the onerror event by assigning a function to window.onerror:
window.onerror = function (msg, url, lineNo, columnNo, error) {
// ... handle error ...
return false;
}
When an error is thrown, the following arguments are passed to the function:
- msg – The message associated with the error, e.g. “Uncaught ReferenceError: foo is not defined”
- url – The URL of the script or document associated with the error, e.g. “/dist/app.js”
- lineNo – The line number (if available)
- columnNo – The column number (if available)
- error – The Error object associated with this error (if available)
The first four arguments tell you in which script, line, and column the error occurred. The final argument, Error object, is perhaps the most valuable. Let’s learn why.
The Error object and error.stack
At first glance the Error object isn’t very special. It contains 3 standardized properties: message, fileName, and lineNumber. Redundant values that already provided to you via window.onerror.
The valuable part is a non-standard property: Error.prototype.stack. This stack property tells you at what source location each frame of the program was when the error occurred. The stack trace can be a critical part of debugging an error. And despite being non-standard, this property is available in every modern browser.
Here’s an example of the Error object’s stack property in Chrome 46:
"Error: foobar\n at new bar (<anonymous>:241:11)\n at foo (<anonymous>:245:5)\n at <anonymous>:250:5\n at <anonymous>:251:3\n at <anonymous>:267:4\n at callFunction (<anonymous>:229:33)\n at <anonymous>:239:23\n at <anonymous>:240:3\n at Object.InjectedScript._evaluateOn (<anonymous>:875:140)\n at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)"
Hard to read, right? The stack property is actually just an unformatted string.
Here’s what it looks like formatted:
Error: foobar
at new bar (<anonymous>:241:11)
at foo (<anonymous>:245:5)
at callFunction (<anonymous>:229:33)
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
Once it’s been formatted, it’s easy to see how the stack property can be critical in helping to debug an error.
There’s just one snag: the stack property is non-standard, and its implementation differs among browsers. For example, here’s the same stack trace from Internet Explorer 11:
Error: foobar
at bar (Unknown script code:2:5)
at foo (Unknown script code:6:5)
at Anonymous function (Unknown script code:11:5)
at Anonymous function (Unknown script code:10:2)
at Anonymous function (Unknown script code:1:73)
Not only is the format of each frame different, the frames also have less detail. For example, Chrome identifies that the new keyword has been used, and has greater insight into eval invocations. And this is just IE 11 vs Chrome – other browsers similar have varying formats and detail.
Luckily, there are tools out there that normalize stack properties so that it is consistent across browsers. For example, raven-js uses TraceKit to normalize error strings. There’s also stacktrace.js and a few other projects.
Browser compatibility
window.onerror has been available in browsers for some time – you’ll find it in browsers as old as IE6 and Firefox 2.
The problem is that every browser implements window.onerror differently. Particularly, in how many arguments are sent to to the onerror listener, and the structure of those arguments.
Here’s a table of which arguments are passed to onerror in most browsers:
| Browser | Message | URL | lineNo | colNo | errorObj |
|---|---|---|---|---|---|
| Firefox 42 | ✓ | ✓ | ✓ | ✓ | ✓ |
| Chrome 46 | ✓ | ✓ | ✓ | ✓ | ✓ |
| Android Browser 4.4 | ✓ | ✓ | ✓ | ✓ | |
| Edge | ✓ | ✓ | ✓ | ✓ | |
| IE 11 | ✓ | ✓ | ✓ | ✓ | ✓ |
| IE 10 | ✓ | ✓ | ✓ | ✓ | |
| IE 9, 8 | ✓ | ✓ | ✓ | ||
| Safari 9 | ✓ | ✓ | ✓ | ✓ | |
| iOS 9 | ✓ | ✓ | ✓ | ✓ |
You’ll notice that the latest Apple browsers – Safari and iOS – don’t support a 5th error object argument. And while the final version of Internet Explorer (11) supports the error object, Microsoft’s latest browser, Edge, does not.
Without the error object, there is no stack trace property. This means that these browsers cannot retrieve valuable stack information from errors caught by onerror.
Polyfilling window.onerror with try/catch
But there is a workaround – you can wrap code in your application inside a try/catch and catch the error yourself. This error object will contain our coveted stack property in every modern browser.
Consider the following helper method, invoke, which calls a function on an object with an array of arguments:
function invoke(obj, method, args) {
return obj[method].apply(this, args);
}
invoke(Math, 'max', [1, 2]); // returns 2
Here’s invoke again, this time wrapped in try/catch, in order to capture any thrown error:
function invoke(obj, method, args) {
try {
return obj[method].apply(this, args);
} catch (e) {
captureError(e); // report the error
throw e; // re-throw the error
}
}
invoke(Math, 'highest', [1, 2]); // throws error, no method Math.highest
Of course, doing this manually everywhere is pretty cumbersome. You can make it easier by creating a generic wrapper utility function:
function wrapErrors(fn) {
// don't wrap function more than once
if (!fn.__wrapped__) {
fn.__wrapped__ = function () {
try {
return fn.apply(this, arguments);
} catch (e) {
captureError(e); // report the error
throw e; // re-throw the error
}
};
}
return fn.__wrapped__;
}
var invoke = wrapErrors(function(obj, method, args) {
return obj[method].apply(this, args);
});
invoke(Math, 'highest', [1, 2]); // no method Math.highest
Because JavaScript is single threaded, you don’t need to use wrap everywhere – just at the beginning of every new stack.
That means you’ll need to wrap function declarations:
- At the start of your application (e.g. in
$(document).readyif you use jQuery) - In event handlers, e.g.
addEventListeneror$.fn.click - Timer-based callbacks, e.g.
setTimeoutorrequestAnimationFrame
For example:
$(wrapErrors(function () { // application start
doSynchronousStuff1(); // doesn't need to be wrapped
setTimeout(wrapErrors(function () {
doSynchronousStuff2(); // doesn't need to be wrapped
});
$('.foo').click(wrapErrors(function () {
doSynchronousStuff3(); // doesn't need to be wrapped
});
}));
If that seems like a heck of a lot of work, don’t worry! Most error reporting libraries have mechanisms for augmenting built-in functions like addEventListener and setTimeout so that you don’t have to call a wrapping utility every time yourself. And yes, raven-js does this too.
Transmitting the error to your servers
Okay, so you’ve done your job – you’ve plugged into window.onerror, and you’re additionally wrapping functions in try/catch in order to catch as much error information as possible.
There’s just one last step: transmitting the error information to your servers. In order for this to work, you’ll need to set up some kind of reporting web service that will accept your error data over HTTP, log it to a file and/or store it in a database.
If this web service is on the same domain as your web application, this is achieved easily by using XMLHttpRequest. In the example below, we use jQuery’s AJAX function to transmit the data to our servers:
function captureError(ex) {
var errorData = {
name: ex.name, // e.g. ReferenceError
message: ex.line, // e.g. x is undefined
url: document.location.href,
stack: ex.stack // stacktrace string; remember, different per-browser!
};
$.post('/logger/js/', {
data: errorData
});
}
Note that if you have to transmit your error across different origins, your reporting endpoint will need to support CORS (Cross Origin Resource Sharing).
Summary
If you’ve made it this far, you now have all the tools you need to roll your own basic error reporting library and integrate it with your application:
- How
window.onerrorworks, and what browsers it supports - How to use try/catch to capture stack traces where window.onerror is lacking
- Transmitting error data to your servers
Of course, if you don’t want to bother with all of this, there are plenty of commercial and open source tools that do all the heavy-lifting of client-side reporting for you. (Psst, you might want to try Sentry.)
That’s it! Happy error hunting.
Capture and report JavaScript errors with window.onerror的更多相关文章
- 【转】window.onerror跨域问题
What the heck is "Script error"? Ben Vinegar/ May 17, 2016 If you’ve done any work with th ...
- How can we listen for errors that do not trigger window.onerror?
原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-win ...
- window.onerror事件用来自定义错误处理
Event reference: https://developer.mozilla.org/en-US/docs/Web/Events http://w3c.github.io/html/ ...
- 用Google Analytics跟踪JavaScript Errors (译)
通过custom events来实施 // Track basic JavaScript errors window.addEventListener('error', function(e) { _ ...
- (转载)JavaScript中的Window窗口对象
(转载)http://www.ijavascript.cn/jiaocheng/javascript-window-65.html 例子: <html> <head> < ...
- window.onerror 应用实例
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp75 window.onerror = function(sMessa ...
- [译]window.onerror事件
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- JavaScript权威指南--window对象
知识要点 window对象及其客户端javascript所扮演的核心角色:它是客户端javascript程序的全局对象.本章介绍window对象的属性和方法,这些属性定义了不同的API,但是只有一部分 ...
- Top 10 JavaScript errors
Top 10 JavaScript errors javascript errors https://rollbar.com/blog/tags/top-errors https://rollbar. ...
随机推荐
- eject - 弹出可移动介质
SYNOPSIS(总览) eject -h.breject [-vnrsfq] [<name>] eject [-vn] -d.breject [-vn] -a on|off|1|0 [& ...
- 一条HTTP的生命之旅(高频面试问题)
当你在浏览器地址栏输入一个URL后回车,将会发生的事情? 原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a- ...
- Win10 启动64位IE浏览器——修改注册表方法
修改注册表[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]下的: "TabProcGrowth"=DWOR ...
- 最短路 || HDU 2066 一个人的旅行
本草的旅行故事(✺ω✺),可以从S个点中的任意一个开始,到达D个点中的任意一个,求最短路 *解法:把草儿的家记成点0,S个点与0的距离为0,然后spfa求最短路 又是改了一万次,①多组数据啊 ②改完多 ...
- Hadoop-01 搭建hadoop伪分布式运行环境
Linux中配置Hadoop运行环境 程序清单 VMware Workstation 11.0.0 build-2305329 centos6.5 64bit jdk-7u80-linux-x64.r ...
- react随笔-1(为什么在react使用jq无法正确渲染组件位置)
今天心血来潮,打开了sublime想玩玩react,然后大家都知道的先引入一大串 就是在百度静态资源库里找到的. 然后贴html代码 对的,没错,就这么一行,毕竟只是测试嘛 然后js代码 大家一定要注 ...
- [Usaco2009 Nov]lights
题目描述: 给出$n$,$m$,表示有$n$盏灯和$m$条奇怪的电线,按下电线一段的灯后另一端会有影响. 求最少按几次. 题解: 高消解异或方程组,得到一堆自由元后搜索自由元状态,然后不断更新答案. ...
- Hibernate入门(1)——环境搭建
什么是Hibernate?为什么使用Hibernate?Hibernate有什么优缺点?网上很多大神的文章又详细又通俗易懂. 步骤: 1.创建数据库表格. 2.编写JavaBean实体类. 3.下载H ...
- 【瞎扯】我的OI之路
这里大概是一些我自己对我的OI之路的一些记录. 2015.11不知道哪一天-- 我听说了"编程". 当时还不懂得啥是信息学竞赛,以为这只是纯粹的程序设计.后来才明白信息学竞赛是算法 ...
- 大数据学习——hive显示命令
show databases; desc t_partition001; desc extended t_partition002; desc formatted t_partition002; !c ...