Backbone.js Error Handling – how do you do it?

In my ears this sounds a bit on the complex side, at least to start with. Backbone.sync will already report errors that you can catch in your models .save() method:

this.mymodel.save(/* ... */, {success: function(model, result, xhr)...,
                              error: function(model, xhr, options)...}

(docs).

If your serverside follows HTTP specs well, the error code is already provided (500 – server error, 404 – model not found, you know..), and even if the server sends an error code it can still send content (perfect for your message). So you basically already have all parameters built in to the HTTP protocol itself. In my experience you get to write less code if you work with the protocol instead of building new layers on top of it.

In your errorcallback above, you probably have good possibilities to call the rest of your system and post an error to some application message bus or similar (via Backbones own event mechanism or some dedicated library).

Leave a Comment