1

Can anyone please provide how to handle the exceptions in JSOM in SharePoint 2013. Currently I am developing a SharePoint 2013 On-Premises portal having JSOM as the approach for coding and while coding I need to put several functions and have to handle exceptions.

Is there any exception handling mechanisms available in JSOM or javascript ? any free frameworks can I use ?

  • 4
    What do you want to catch what standard try-catch can't do? – Danny '365CSI' Engelman Dec 8 '16 at 9:09
  • Would like to know what if an exception occurs in my CRUD operations in UI against SPList & SPDocument Libraries. Will it log the exception in LOGS folder's log files automatically? Similar to the implementation of SPDiagnosticsServiceBase class available in SSOM. – prasadwt Dec 8 '16 at 9:13
  • 2
    (I haven't done serious back-end in 10 years..so might be wrong here) I wouldn't think exceptions are logged server-side because JSOM is Front-End technology calling an endpoint. If exceptions where logged server-side, us Front-End developers would have no way of accessing the info. – Danny '365CSI' Engelman Dec 8 '16 at 9:31
  • 2
    Before you deep dive into JSOM, check out Microsofts work which they use in modern developments: github.com/SharePoint/PnP-JS-Core – Danny '365CSI' Engelman Dec 8 '16 at 11:25
  • 1
    They are the same REST endpoints. – Danny '365CSI' Engelman Dec 8 '16 at 11:43
2

Everything in JSOM is client side. So the exceptions you get are not stored server side. You can use the on fail method as below to store & check exceptions in the console itself. Check below code snippet :

function getItems(camlQuery){

     var context = SP.ClientContext.get_current();
     var web = context.get_web();
     var list = context.get_web().get_lists().getByTitle('Test');
     var items = list.getItems(camlQuery, 'AllItems');

     context.load(items);
     context.executeQueryAsync(Function.createDelegate(this, onSuccess),   
     Function.createDelegate(this, onFailure));  

}

function onSuccess(sender, args) {
   //...success
   //execute your code
}

function onFailure(sender, args) {
    console.log('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());    
}

This will give you the error message as well as the stack trace in the console. To check the message, press F12 (opens developer tools) and click on console tab.

onFailure method can be used across different functions.

  • function onSuccess(sender, args) { } can i use try {} catch () {} finally {} here ? – prasadwt Dec 8 '16 at 11:11
  • 1
    No, they are not Promises, you have to wrap it in a Promise yourself. And Function.createDelegate is oldskool below IE9 code, use .bind – Danny '365CSI' Engelman Dec 8 '16 at 11:24
1

Try catch does not work that well with async, you could use SP.ExceptionHandlingScope when you work against SharePoint data.

Simple example creating ClientContext against a none existing site:

var ctx = new SP.ClientContext("https://i-dont-exists");
ctx.load(ctx.get_web(), "Title");

var exceptionScope = new SP.ExceptionHandlingScope(ctx);  
var startScope = exceptionScope.startScope();  

var tryScope = exceptionScope.startTry();
       var web = ctx.get_web();
tryScope.dispose();  

var catchScope = exceptionScope.startCatch();  

    // DO stuff in catch
    ctx = SP.ClientContext.get_current()
    ctx.load(ctx.get_web(), "Title");

catchScope.dispose();  

var finallyScope = exceptionScope.startFinally();  
    // DO Stuff in finally                          

finallyScope.dispose();  

startScope.dispose();  

ctx.executeQueryAsync(function() { 
    console.log(ctx.get_web().get_title());
}, function(s, args) { 
    console.log('Err: ' + args.get_message()); 
});  
-1

You can use:

try {
    ...
}
catch (Exception) {
    //Get error code and act
}
finally {
    ...
}

Good article here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.