Send javascript errors by mail

I'm running a Django-powered site for a closed user group and added a bit of JavaScript magic here and there (mainly Prototype and Tooltip).

Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has solved this problem but didn't find anything, so here's my take: Just send any error using Ajax (here: using Prototypes Ajax abstraction) to the server

JAVASCRIPT:
  1. onerror = Extranet.mailError;
  2. function mailError(msg, url, line) {
  3.   var postBody = 'url=' + url + '&line=' + line + '&message=' + escape(msg) + '&useragent=' + escape(navigator.userAgent) + '&user=' + escape(user_name);
  4.   var myAjax = new Ajax.Request('/api/jserror/', {method: 'post', postBody: postBody});
  5. }

user_name is a JavaScript variable holding the Django username (so I know whom I can inform when the error is fixed).

On the server side, I just send me mails containing the JavaScript error message, the username and the user agent:

PYTHON:
  1. def jserror(request):
  2.   from django.core.mail import mail_admins
  3.   omit_messages = ['pointerobj is not defined', 'tipobj is not defined', 'ns6 is not defined', 'enabletip is not defined']
  4.   if request.POST.get('message', '') not in omit_messages:
  5.     message = """url: %s (%s)
  6. %s
  7. user-agent: %s
  8. username: %s
  9. """ % (request.POST.get('url', ''), request.POST.get('line', ''), request.POST.get('message', ''), request.POST.get('useragent', ''), request.POST.get('user', ''))
  10.     mail_admins("javascript error", urldecode(message))
  11.   return HttpResponse()

Yeah, that's all very trivial but I wonder what other solutions exist for this problem...

2 Comments

  1. steve Says:

    Very nice. I've wanted to do this for some time, but mentally I got hung up on the "but JavaScript can't send email" concept, that this type of thing just never came to fruition. :-(

    I quite like your implementation, and plan to make my own (PHP wise), possibly including a bit more stack trace (from browsers that support it (e.g. Firefox/Safari))

    I guess my other fear on this one, has been the potential for a flood of email... but I suppose that would only happen if tons of users hit (or the same user repeatedly) the issue... alas, a few dozen emails is nothing, if I can get a "real-time" bug reporting system in place, such that I can find/fix a problem even before the end user has a chance to "officially" report it.

    Thanks for the inspiration!

  2. philipp.keller Says:

    Thanks!

    I had the same fear: A flood of emails will welcome me every morning. But as I just have about 90 users I got 20 mails maximum a day. The positive effect is, that if I get many mails, I know my users have a problem as a javascript error most possibly hinders them to use my site properly. And as they often don't report errors because they think they are doing something wrong I can get a lot more fixed this way.

    Whenever you have your implementation, let me know. I wanted to do the stack trace as well but smelled too much work.

Leave a Reply