Handling Errors in Sitecore Sidekick

Just a quick one before I head to bed. I’m still very much enjoying tinkering with Sitecore Sidekick. However because of the way it posts messages back and forth – sometimes you’re going to need to handle bugs in your code.

Here’s what happens in my last bit of code if an unhandled error bubbles up tot he surface:

error

I don’t really want a load of errors appearing in the console of the browser – so I’ll trap that pesky error:

try
{
ReturnJson(context, RunScript(context));
throw new Exception("This is not the code you are looking for.");
}
catch (Exception ex)
{
ReturnJson(context, new Error { ErrorMessage = $"{ex.Message}. This is the code you are looking for!" });
}

Here we’re returning an error class instead of the normal response. It’s a simple class for the purposes of this blog:

public class Error
    {
        public string ErrorMessage { get; set; }
    }

 
Then all that’s left to do is pick that up on the front end. You’ll need something to catch it in the controller (using our poweshell tool from the previous blog post) note that the response data is in Camel Case.

PSfactory.runScript().then(function (response) {
		        if (response.data.ErrorMessage) {
		            $scope.error = true;
		            $scope.errorMessage = response.data.ErrorMessage
		        } else {
		            vm.data = response.data;
		        }

And then we need to show this error in the .scs file:

<div ng-controller="powershelltoolcontroller as vm">
  <div ng-show="error">
    An unhandled error occured: {{errorMessage}}
  </div>

Sorted!

Next blog post will be a bit meatier (like a new £5 note). I’ll show you how to fire a long running job and periodically poll the job status and display it in your sidekick tool. It’s pretty sweet!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s