Hello kind readers…
JSOM… or the JavaScript client object model that is one of the major features of SharePoint 2010 is really a great API for doing loads of functionality without owning a Visual studio :)
I really love the JSOM and use it very often and with time I always learn something new about it… the one thing I wanted to share with all was about the error handling/trapping with JSOM, as more and more people are moving towards using the client object model, knowing the right error handling techniques could really help in better coding practices and more robust code.
Mostly when we make ASync calls with client object model… we provide a error function to get any errors that might have come in the query and it gives out a message saying "cannot complete operation" or something… well not always would you have a clear idea on what happened at the server end to troubleshoot the error, for that you need a proper server side error message.
Now with JSOM you can still get that error message and this is something that can be REALLY helpful at times… following are the two ways of handling error in JSOM, one is normal usual Client side way, other is when you trap sever side error message,
Normal way:
//get clientcontext
//get some web
//get some list
//make some query
Ctx.executeQueryAsync(onQueySucceeded,onQueyFailed)
funciton onQueySucceeded(){...}
funciton onQueyFailed() {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
This way could give some information of course but not always the server end information about the error which you might need for troubleshooting or understanding the cause of your functionality failure.
SP namespace itself provides a way to handle the error scenarios which can give you server side error information as well, and it is nice one… following is the way to use it..,
var _siteObjects ={
errorScope: null,
clientCtx:null,
};
_siteObjects.clientCtx = //get the current client context or the site relative client context
_siteObjects.errorScope = new SP.ExceptionHandlingScope(_siteObjects.clientCtx);
//scope for handling starts
var startScope = _siteObjects.errorScope.startScope();
//Start the try block for trapping server error
var tryBlock = _siteObjects.errorScope.startTry();
// you query and other operations here like getting the web and list and items query
//load the objects in context to get from server
Ctx.load('objects here');
//end try block
tryBlock.dispose();
//start catch block
var catchBlock = _siteObjects.errorScope.startCatch();
//place holder for server processing on error if any
//like if you want to delete the item being created or editing if any errors
//or any other thing you would want if the error was thrown
//NOTE - server side things will only execute if the error was thrown in corresponding try block
catchBlock.dispose(); //end catch block
startScope.dispose(); //exception scope ends
//now you call your execute query with normal way
_siteObjects.clientCtx.executeQueryAsync(onQueySucceeded,onQueyFailed)
function onQueySucceeded(sender, args) {
if (_siteObjects.errorScope.get_hasException()) {//If there was a server side error log it in the div from exception object
jQuery(_containerId).append("<div message='" + _siteObjects.errorScope.get_errorMessage() + "'>An error has occured.</div>");
}
Now this errorMessage is the error that was thrown at server end by the code and you could get the exact error from this to trouble shoot.
This is just another way of error handling when working with Client object model in JS and could give you more robust ways of handling different situations based on your needs and requirements.
Hope this helps somebody….
- "T"