Backbone.js: Better error visibility – Mr. Joel Kemp

Here’s a quick tip that should save you some hair and a few debugging hours when dealing with errors generated during Backbone.Events pub/sub.

Monkeypatch Backbone.trigger with a try/catch to see errors that were previously trapped like so:

  var oldTrigger = Backbone.trigger;  Backbone.trigger = function () {    try {      oldTrigger.apply(Backbone, arguments);    } catch (e) {      console.error(e);    }  };

It’s a simple wrapping that should be placed at the top of your app’s initialization. Note the use of console.error (Supported in IE8 and every other browser) which will print the error object to the console. If you’re using a javascript error monitoring tool (I personally use Muscula), they’ll log it without a problem.