---
title: "Error"
description: "Error"
url: "https://www.contentstack.com/docs/developers/sdks/content-delivery-sdk/java/reference/error"
product: "Contentstack"
doc_type: "guide"
audience:
  - developers
  - admins
version: "current"
last_updated: "2026-11-13"
---

# Error

## Error

Error holds the details of a request that failed. The Java Delivery SDK does not throw an exception when the Content Delivery API (CDA) rejects a request or when the request does not complete. It passes an Error to the request's callback instead. Use this class to

*   tell a failed request from a successful one
*   read the error code and message the API returned
*   read the field-level details of a rejection

Every callback in the [Callbacks](/docs/developers/sdks/content-delivery-sdk/java/reference/callbacks) class receives an Error argument. The argument is null when the request succeeds.

## getErrorCode

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

```
Returns: int. The Contentstack error code.
ValidationThere is no client-side validation. getErrorCode returns a value that the SDK stored before it called the callback.getErrorCode does not throw.BehaviorWhen the API rejects the request with a JSON body, getErrorCode returns the body's error_code field, for example 141. The Content Delivery API Errors reference lists the codes.The value is not the HTTP status code. The SDK does not expose the HTTP status of a failed request.getErrorCode returns 0 in each of the five cases below:The error body has no error_code, or its value is not a number.The error body is empty or is not JSON.The request times out.Any other network failure stops the request.The API returns a success status with a body that is not JSON.On an Error that you create with new Error(), getErrorCode returns 0.Branch on this value rather than on the text of getErrorMessage.ExampleBasic usage: print the code of a failed entry fetchimport com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class PrintErrorCode {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                System.out.println(entry.getTitle());
            }
        });
    }
}Error handling: separate an API rejection from a failure with no API responseimport com.contentstack.sdk.Asset;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchResultCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ClassifyAssetError {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Asset asset = stack.asset("<ASSET_UID>");

        asset.fetch(new FetchResultCallback() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error == null) {
                    System.out.println(asset.getFileName());
                } else if (error.getErrorCode() == 0) {
                    // No usable API response: a timeout, a network failure, or an unreadable body
                    System.out.println("Try again later: " + error.getErrorMessage());
                } else {
                    // The API rejected the request
                    System.out.println("API error " + error.getErrorCode() + ": " + error.getErrorMessage());
                }
            }
        });
    }
}
```

## getErrorDetail

getErrorDetail returns the field-level details that the Content Delivery API (CDA) sent with a rejected request.

```
Returns: String. The error details as JSON text.
ValidationThere is no client-side validation. getErrorDetail returns a value that the SDK stored before it called the callback.getErrorDetail does not throw.BehaviorWhen the API rejects the request with a JSON body that has an errors field, getErrorDetail returns that field's value as JSON text. Parse it with new JSONObject(...) from org.json to read the individual fields.In every other failure, getErrorDetail returns the sentence No additional error details available. That covers a body without errors, an empty or non-JSON body, a timeout, and any other network failure.The sentence is not JSON, so new JSONObject(...) throws JSONException on it. Check that the value starts with { before you parse it, as the second example below does.On an Error that you create with new Error(), getErrorDetail returns null.ExampleBasic usage: print the details of a failed entry fetchimport com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class PrintErrorDetail {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    System.out.println("Details: " + error.getErrorDetail());
                    return;
                }
                System.out.println(entry.getTitle());
            }
        });
    }
}Edge case: parse the details only when they hold a JSON objectimport com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;

public class ParseErrorDetail {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error == null) {
                    System.out.println(entry.getTitle());
                    return;
                }
                String detail = error.getErrorDetail();
                if (detail != null && detail.startsWith("{")) {
                    JSONObject fields = new JSONObject(detail);
                    for (String field : fields.keySet()) {
                        System.out.println(field + ": " + fields.get(field));
                    }
                } else {
                    System.out.println(error.getErrorMessage());
                }
            }
        });
    }
}
```

## getErrorMessage

getErrorMessage returns the text that describes why a request failed.

```
Returns: String. The error message text.
ValidationThere is no client-side validation. getErrorMessage returns a value that the SDK stored before it called the callback.getErrorMessage does not throw.BehaviorWhen the API rejects the request with a JSON body, getErrorMessage returns the body's error_message field. When the body has no error_message, it returns An unknown error occurred.When the error body is not JSON, getErrorMessage returns the body text as it arrived.When the error body is empty, getErrorMessage returns Unexpected error: No response received from server.When the request times out, the text starts with Request timed out: and continues with the message of the timeout exception from OkHttp (com.squareup.okhttp3:okhttp).When any other network failure stops the request, the text starts with IO error occurred:.When the API returns a success status with a body that is not JSON, getErrorMessage returns Invalid JSON response. Check the server response format and try again.On an Error that you create with new Error(), getErrorMessage returns null.Use the text for logs and display. To decide what to do next, read getErrorCode, because the SDK can reword its own messages in any release.ExampleBasic usage: print the message of a failed entry fetchimport com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class PrintErrorMessage {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");

        entry.fetch(new EntryResultCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, Error error) {
                if (error != null) {
                    // The API rejected the request, or it did not complete
                    System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
                    return;
                }
                System.out.println(entry.getTitle());
            }
        });
    }
}Edge case: report a failure that has no API responseimport com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

public class ReportNetworkFailure {
    public static void main(String[] args) throws IllegalAccessException {
        Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
        Query query = stack.contentType("<CONTENT_TYPE_UID>").query();

        query.find(new QueryResultsCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
                if (error != null && error.getErrorCode() == 0) {
                    // Code 0: a timeout, a network failure, or an error body the SDK could not read
                    System.out.println("No usable API response: " + error.getErrorMessage());
                    return;
                }
                if (error != null) { System.out.println(error.getErrorMessage()); return; }
                System.out.println(queryResult.getResultObjects().size() + " entries");
            }
        });
    }
}
```

## Error | Java Delivery SDK | Contentstack

The Error class in the Java Delivery SDK carries the message, error code, and details of a failed request to the request's callback.