Reporting JavaScript Errors

By Josh Wright on March 2011 in tips


Two days ago, Maria hits me with the old "Your website doesn't work on my computer" shtick. If you're a web developer, you know the drill:

  1. What page was she on?
  2. What was the problem?
  3. What browser/OS were they using?
  4. Duplicate the problem on my computer

Ends up being an IE 7 issue with css or javascript, but I couldn't duplicate the problem. She lives about 1000 miles away, so I can't exactly pop over to her desk. So how do you solve a problem like Maria?

Email Client-Side Errors

The idea is to catch all javascript errors on a webpage and email them to myself. I've been doing something similar with server-side errors for about a year and it's been glorious.

Here's the javascript:

window.onerror = function (msg, url, line) {


    "<%= Url.Action("New", "Error") %>",

    { msg : msg + " on line " + line }



Make sure it's the first script reference in your webpage, otherwise any errors that happen before it's loaded won't be reported.

The server side handler for this ajax is trivial and probably not helpful, but here it is none-the-less:

public ActionResult NewFromClient(string msg)


new MailerService().SendClientSideError(msg);

return new EmptyResult();


By the way, my favorite error is "null is 'null' or not an object"? Admittedly, errors like this don't tell you much besides something was null, but at least you get a line number, right?