Error

View as Markdown

Error

Error maps the JSON error body that the Content Management API (CMA) returns with a rejected request. The Java Management SDK does not throw when the API rejects a request, and it does not create an Error for you. You parse the error body into one.

Use this class to read the three parts of an error body:

  • the error message
  • the Contentstack error code
  • the field-level details of the rejection

getErrorCode

getErrorCode returns the error code that the Content Management API (CMA) sent with a rejected request.

Returns: int. The Contentstack error code.

Validation

  • There is no client-side validation. getErrorCode does not throw.

Behavior

  • getErrorCode returns the error_code value of the parsed error body, for example 141, or 0 when the body has none.
  • The value is not the HTTP status code. Read the status with response.code().
  • The SDK does not set the value. It comes from your Gson.fromJson call or from setErrorCode.
  • See Content Management API Errors for the codes.

Example

Basic usage: parse the error body of a failed request

import com.contentstack.cms.Contentstack;
import com.contentstack.cms.models.Error;
import com.google.gson.Gson;
import okhttp3.ResponseBody;
import retrofit2.Response;

import java.io.IOException;

public class PrintErrorCode {
    public static void main(String[] args) throws IOException {
        Contentstack client = new Contentstack.Builder().build();

        Response<ResponseBody> response = client.stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
                .contentType("<CONTENT_TYPE_UID>")
                .entry("<ENTRY_UID>")
                .fetch()
                .execute();
        if (!response.isSuccessful()) {
            // The API rejected the request. Read the body once and parse it.
            Error error = new Gson().fromJson(response.errorBody().string(), Error.class);
            System.out.println("HTTP " + response.code() + ", error code " + error.getErrorCode());
            return;
        }
        System.out.println(response.body().string());
    }
}

getErrorMessage

getErrorMessage returns the message that the Content Management API (CMA) sent with a rejected request.

Returns: String. The error message text.

Validation

  • There is no client-side validation. getErrorMessage does not throw.

Behavior

  • getErrorMessage returns the error_message value of the parsed error body, or null when the body has none.
  • The SDK does not set the value. It comes from your Gson.fromJson call or from setErrorMessage.
  • The message is for people. Branch on getErrorCode in code, because the API can reword a message.

Example

Basic usage: parse the error body of a failed request

import com.contentstack.cms.Contentstack;
import com.contentstack.cms.models.Error;
import com.google.gson.Gson;
import okhttp3.ResponseBody;
import retrofit2.Response;

import java.io.IOException;

public class PrintErrorMessage {
    public static void main(String[] args) throws IOException {
        Contentstack client = new Contentstack.Builder().build();

        Response<ResponseBody> response = client.stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
                .contentType("<CONTENT_TYPE_UID>")
                .entry("<ENTRY_UID>")
                .fetch()
                .execute();
        if (!response.isSuccessful()) {
            // The API rejected the request. Read the body once and parse it.
            Error error = new Gson().fromJson(response.errorBody().string(), Error.class);
            System.out.println("Request failed: " + error.getErrorMessage());
            return;
        }
        System.out.println(response.body().string());
    }
}

getErrors

getErrors returns the details that the Content Management API (CMA) sent about each field or value that caused a rejection.

Returns: Object. The field-level error details.

Validation

  • There is no client-side validation. getErrors does not throw.

Behavior

  • getErrors returns the errors value of the parsed error body, or null when the body has none.
  • The type is Object because the shape varies. Gson maps a JSON object to a Map and a JSON array to a List. Check the type with instanceof before you cast.
  • The SDK does not set the value. It comes from your Gson.fromJson call or from setErrors.

Example

Basic usage: parse the error body of a failed request

import com.contentstack.cms.Contentstack;
import com.contentstack.cms.models.Error;
import com.google.gson.Gson;
import okhttp3.ResponseBody;
import retrofit2.Response;

import java.io.IOException;

public class PrintErrorDetails {
    public static void main(String[] args) throws IOException {
        Contentstack client = new Contentstack.Builder().build();

        Response<ResponseBody> response = client.stack("<API_KEY>", "<MANAGEMENT_TOKEN>")
                .contentType("<CONTENT_TYPE_UID>")
                .entry("<ENTRY_UID>")
                .fetch()
                .execute();
        if (!response.isSuccessful()) {
            // The API rejected the request. Read the body once and parse it.
            Error error = new Gson().fromJson(response.errorBody().string(), Error.class);
            Object details = error.getErrors();
            if (details instanceof java.util.Map) {
                // Each key names a field. Each value describes what is wrong with it.
                System.out.println("Field errors: " + details);
            } else {
                System.out.println("Request failed: " + error.getErrorMessage());
            }
            return;
        }
        System.out.println(response.body().string());
    }
}