Handling errors in datajs

Today I want to talk a bit about how we handle errors in datajs.

Every operation that datajs runs asynchronously, whether a read or a general request, has both a success and an error callback. These can be passed in explicitly when the function is invoked, and this pretty much always done with the success callback.

Often, however, there is little to do in the face of an error rather than letting the user know that things didn't go well, perhaps offer a message with more details, and letting the user retry or perhaps fix something before retrying. At this point, writing an error callback for every function invocation is a bit of a pain, so datajs offers the OData.defaultError.

This field has a function that is used when an explicit function is omitted from a call and is passed an error. By default, it will simply throw the error object, which depending on your browser and how you're running it, may trigger the debugger that will let you look at why things failed.

As shown in the walk-through from a few days ago, however, it's a very convenient location to re-enable controls (if you disabled them while waiting for a server response), and display a message to the user.

OData.defaultError = function (err) {
    $("button").attr("disabled", false);


Skip to main content