Contentstack SaaS Headless CMS .NET SDK Exception Management

This blog post explains how you can access information about errors that occur when using the .NET SDK for the Contentstack SaaS headless content management system.

For background information about accessing error data from the underlying .NET HttpWebRequest class, see this previous blog post:

If an error occurs while calling a Contentstack HTTP API, the server returns an HTTP error response. The .NET SDK may convert the error response to a ContentstackException that exposes a few helpful properties:

  • ErrorCode: Contentstack error status code
  • ErrorMessage: Error message from Contentstack
  • StatusCode: HttpStatusCode returned from API request
  • Errors: Dictionary<string,object> created from JSON error response from Contentstack

In cases where Contentstack cannot generate the error response, the SDK may generate a WebException.

In either case, the exception may appear as an InnerException within an AggregateException, where that InnerException could have an InnerException.

My general strategy for exceptions is not to trap them. If I need to trap them for some purpose, such as diagnostics or logging, I rethrow the original exception unless my code can completely recover from the error somehow. For me, this somehow justifies trapping all exceptions in a single catch block rather than writing a catch block for each type of exception. This catch block iterates nested exceptions, renders what it assumes to be the most useful information from the first WebException or ContentstackException that it finds or from the original exception otherwise, and then rethrows the original exception.

When you pass a bad stack identifier, Contentstack returns something like this:

{"error_message":"We can't find that Stack. Please try again.","error_code":109,"errors":{"api_key":["is not valid."]}}

The error management block renders this response as follows.

Contentstack.Core.Internals.ContentstackException : We can't find that Stack. Please try again.
Error code: 109
Error message: We can't find that Stack. Please try again.
Status code: PreconditionFailed
Error : api_key : [
  "is not valid."
   at Contentstack.Core.ContentstackClient.GetContentTypes(Dictionary`2 param)
Unhandled exception. System.AggregateException: One or more errors occurred. (Exception of type 'Contentstack.Core.Internals.ContentstackException' was thrown.)
 ---> Contentstack.Core.Internals.ContentstackException: Exception of type 'Contentstack.Core.Internals.ContentstackException' was thrown.
   at Contentstack.Core.ContentstackClient.GetContentTypes(Dictionary`2 param)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at cscrawl.Program.Main(String[] args) in c:\temp\cscrawl\Program.cs:line 50

One thought on “Contentstack SaaS Headless CMS .NET SDK Exception Management

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: