SALES: 1-800-867-1380
Microsoft Azure
Collapse the table of content
Expand the table of content

Error handling

 

Updated: June 16, 2015

By default in a JavaScript backend mobile service, server scripts write errors to the log. In addition, operations that send a response, such as table operations and custom APIs write the error result to the response. To provide more control over script execution, you can also control error behaviors by specifying an error handler in your script. You typically implement explicit error handling when some sort of compensating action is possible or when you want to write more detailed information to the log using the global console object.

The following insert operation example augments the standard error log by writing the userId value of the current user in addition to the error object (err) itself.

function insert(item, user, request) {
    request.execute({
        error: function(err) {
            console.error("Error occurred for user '%s'. Details:", user.userId, err);
            request.respond();
        }
    });
}

For a custom API, write the appropriate error code directly to the response. The following GET request implementation returns a 500 error.

exports.get = function(request, response) {
    // This example just returns a 500 HTTP response.
    response.send(500, "An error occurred in your custom API code.");
};

For more information, see the Mobile Services script reference

Show:
© 2016 Microsoft