visionmedia/mocha — pull request #665 by iammerrick (2012-11-21 03:15:09+00:00)
Title: window.onerror and Asynchronous Errors

## The problem:

Currently asyncronous errors in the browser are horrific, we don't get access to an Error object but just a string. This means that every error thrown in a callback within a Mocha test like this:

```javascript

describe('sad day', function() {
it('should have a nice trace', function(done) {
setTimeout(function() {
throw new Error('A little bit better.');
done();
}, 500);
});
});

```

The problem is Mocha wraps this and warns you not to throw strings, but the end user isn't throwing a string, `window.onerror` just blows! Unfortunately we lose access to the stack trace as well with no handle of getting it back. To help make this suck a little less I've taken the url and line parameters thrown with `window.onerror` and added them to the message to make it a bit more useful, so now instead of the top error being from Mocha.js some crazy line number, the top line shows where your actual error is from.

## Before the pull request:
![Before Pull Request](http://f.cl.ly/items/2B1a0v2O13061n0m440q/useless.png)

## After the pull request:
![After Pull Request](http://f.cl.ly/items/20332E410Z1r3D1V2B35/useful.png)

Notice how after the pull request we get the file url and the line number. Albeit no stack trace cause thats currently the name of the game with browsers as far as I know.