ContentstackErrorException

View as Markdown

ContentstackErrorException

ContentstackErrorException is the exception the SDK throws when the Content Management API (CMA) rejects a request, in the Contentstack.Management.Core.Exceptions namespace.

When to Use This Class: Catch it around every method that sends a request. Read StatusCode, ErrorCode, ErrorMessage, and Errors to see what the API rejected.

Conceptual Role: Acts as the error result of every request. It extends System.Exception directly, not ContentstackException.

Returns: ContentstackErrorException. The details of a rejected request.

Class-Level Notes

When the SDK throws it.

  • The SDK throws it for any HTTP status outside 2xx, after the automatic retries end. See the ContentstackClientOptions class page for the retry rules.
  • The SDK does not throw it for a client-side check, such as a missing authtoken or UID. Those throw InvalidOperationException or ArgumentException before any request.
  • The SDK does not throw it for a network failure, such as a timeout or a refused connection. Those throw the original .NET exception, for example TaskCanceledException or HttpRequestException.

Where the values come from.

  • The SDK reads error_message, error_code, and errors from the JSON body of the error response. A body that is not JSON leaves ErrorCode at 0 and puts a short description in ErrorMessage.
  • The SDK fills StatusCode, ReasonPhrase, and Header from the HTTP response.
  • Message repeats ErrorMessage. Read ErrorMessage.

Async methods.

  • await the task to catch this exception directly. Reading .Result or calling .Wait() wraps it in AggregateException.

Class-Level Properties

PropertyTypeAccessDefaultDescription
StatusCodeHttpStatusCodeRead/writeThe response statusHTTP status of the error response, such as 422.
ErrorMessagestringRead/writeEmpty stringThe error_message value of the response body.
ErrorCodeintRead/write0The error_code value of the response body.
ErrorsDictionary<string, object>?Read/writenullThe errors object of the body, keyed by field name. Each value is a JsonElement.
Messagestring?Read/writeSame as ErrorMessageHides Exception.Message and repeats ErrorMessage.
ReasonPhrasestring?Read/writeThe response reasonHTTP reason phrase, such as Unprocessable Entity.
HeaderHttpResponseHeaders?Read/writeThe response headersHeaders of the error response.
RetryAttemptsintRead/write0Not set by the SDK in this version. Always 0.
OriginalErrorException?Read/writenullNot set by the SDK in this version. Always null.
IsNetworkErrorboolRead/writefalseNot set by the SDK in this version. Always false.

Method Index

Method NameReturnsDescription
CreateExceptionA new exception built from the error response.Builds the exception from an HTTP response.

Class-Level Snippet

This snippet lists the content types of a stack and prints each field error when the API rejects the request.

using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Query().Find();
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");

    foreach (KeyValuePair<string, object> error in e.Errors ?? new Dictionary<string, object>())
    {
        Console.WriteLine($"{error.Key}: {error.Value}");
    }
}

Additional Resource Refer to Content Management API Errors for the full error code list.

Methods

Each method of the ContentstackErrorException class has its own section below, with its parameters, validation rules, behavior, and examples.

CreateException

CreateException builds a ContentstackErrorException from an HTTP response that carries a Content Management API (CMA) error.

NameTypeDescription
response (required)HttpResponseMessage

The error response to read.

Default: Not applicable

Returns: ContentstackErrorException. A new exception built from the error response.

Validation

  • Throws NullReferenceException if response is null.
  • A body that is not JSON raises no error. CreateException sets ErrorMessage to a short description of the problem instead.

Behavior

  • Client-side only. CreateException sends no request. The SDK calls it for every error response, and you rarely need to call it yourself.
  • Reads the body as text, blocking the calling thread, and deserializes error_message, error_code, and errors from it.
  • Sets StatusCode, Header, and ReasonPhrase from response.
  • Returns the exception without throwing it.

Example

Basic usage: build the exception from your own HTTP call

using System;
using System.Net;
using System.Net.Http;
using Contentstack.Management.Core.Exceptions;

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.UnprocessableEntity)
{
    Content = new StringContent("{\"error_message\": \"Entry creation failed.\", \"error_code\": 119}")
};

ContentstackErrorException error = ContentstackErrorException.CreateException(response);
Console.WriteLine($"{(int)error.StatusCode} {error.ErrorCode} {error.ErrorMessage}");