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

# Error

## Error

**Warning:** Contentstack has deprecated the Dart Delivery SDK for new projects. Existing apps can keep using it. For new Dart or Flutter projects, call the [Content Delivery API](/docs/developers/apis/content-delivery-api) directly with an HTTP client.

Error holds the details of a Content Delivery API error as typed fields. The SDK never creates an Error itself. When the API rejects a request, the SDK returns the decoded error body as a Map, and you build an Error from that map. Use this class to

*   read the error message and the error code as typed values
*   read the field-level details that the API returns for some errors
*   throw the API error as an exception in your app

contentstack.Error({...}) takes the named arguments below. In most code, build it with [fromJson](/docs/developers/sdks/content-delivery-sdk/dart/reference/error#error-fromjson) instead.

Name

Type

Description

errorMessage

String?

Message that describes the failure. Defaults to null.

errorCode

int?

Contentstack error code. Defaults to null.

errors

Map?

Field-level details. Defaults to null.

## Class-Level Notes

**The SDK never returns an Error.**

*   A request method returns the API error body as a Map with the keys error\_message, error\_code and, for some errors, errors. A check such as if (response is contentstack.Error) never matches.
*   Check the result for an error\_code key, then pass the map to [fromJson](/docs/developers/sdks/content-delivery-sdk/dart/reference/error#error-fromjson) when you want typed fields.

**Write the class with the SDK prefix.**

*   Error shares its name with the Error class of dart:core. Import the SDK with as contentstack and write contentstack.Error, so the name resolves to the SDK class.

**Error is an exception.**

*   Error implements Exception. You can throw an Error and catch it with on contentstack.Error catch (e).

**Client-side only.**

*   Error only stores values. Its methods send no request.

**Additional Resource:** Refer to [Content Delivery API Errors](/docs/developers/apis/content-delivery-api#errors) for the full error code list.

## Class-Level Properties Table

Property

Type

Access

Default

Description

errorMessage

String?

Read-only

null

The message of the error, from the error\_message key.

errorCode

int?

Read-only

null

The Contentstack error code, from the error\_code key.

errors

Map?

Read-only

null

The details for each field that failed, from the errors key.

## Method Index

Method Name

Returns

Description

[fromJson](/docs/developers/sdks/content-delivery-sdk/dart/reference/error#error-fromjson)

A new error that holds the values of the error map.

Builds an Error from the map a request returns.

[toJson](/docs/developers/sdks/content-delivery-sdk/dart/reference/error#error-tojson)

The error as a JSON map.

Converts the error back to a map.

## Class-Level Snippet

This snippet fetches an entry and, when the API rejects the request, throws the error as a contentstack.Error and logs its fields.

```
import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final entry = stack.contentType('<CONTENT_TYPE_UID>').entry(entryUid: '<ENTRY_UID>');

  try {
    final response = await entry.fetch();
    if (response is Map<String, dynamic> && response.containsKey('error_code')) {
      // The API rejected the request
      throw contentstack.Error.fromJson(response);
    }
    print(response['entry']['title']);
  } on contentstack.Error catch (error) {
    print('Code: ${error.errorCode}');
    print('Message: ${error.errorMessage}');
    print('Details: ${error.errors}');
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}
```

## Methods

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

## fromJson

fromJson builds an Error from the error map that a request returns when the API rejects it.

```
Returns: Error. A new error that holds the values of the error map.
ValidationThrows TypeError if error_message is not a string, error_code is not an integer, or errors is not a map, because fromJson casts each value to its field type.A missing key raises no error. The matching field is null. A map with none of the three keys, such as a successful response, gives an Error with every field null.json must have the static type Map<String, dynamic>. A result narrowed only with is Map has the type Map<dynamic, dynamic> and does not compile as the argument. Test it with is Map<String, dynamic> instead.BehaviorClient-side only. fromJson reads three keys and makes no request.error_message becomes errorMessage.error_code becomes errorCode.errors becomes errors.fromJson does not read any other key of the map.fromJson is a factory constructor. Call it on the class, as contentstack.Error.fromJson(response).ExampleBasic usage: build an Error from a rejected requestimport 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final asset = stack.asset('<ASSET_UID>');

  try {
    final response = await asset.fetch();
    if (response is Map<String, dynamic> && response.containsKey('error_code')) {
      // The API rejected the request
      final error = contentstack.Error.fromJson(response);
      print('Request failed: ${error.errorCode} ${error.errorMessage}');
    } else {
      print(response['asset']['url']);
    }
  } catch (e) {
    // Thrown for a missing UID, a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}Read the field-level detailsimport 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');

  final response = await stack.syncToken('<SYNC_TOKEN>');
  if (response is Map<String, dynamic> && response.containsKey('error_code')) {
    final error = contentstack.Error.fromJson(response);
    error.errors?.forEach((field, messages) => print('$field: $messages'));
  }
}
```

Error body that the request returned.

## toJson

toJson converts the error back to a map with the keys the API uses.

This method takes no parameters.

```
Returns: Map. The error as a JSON map.
ValidationThere is no client-side validation. toJson cannot fail.BehaviorClient-side only. toJson makes no request.It returns a Map<String, dynamic> with three keys, error_message, error_code and errors. A field that is null appears with a null value.Pass the map to jsonEncode from dart:convert to log the error or send it to your own service.ExampleBasic usage: log an error as JSONimport 'dart:convert';

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final error = contentstack.Error(
    errorMessage: 'The requested entry does not exist.',
    errorCode: 141,
  );

  print(jsonEncode(error.toJson()));
}
```

## Error | Dart Delivery SDK | Contentstack

The Error class in the Dart Delivery SDK holds the message, the error code and the field details of a Content Delivery API error, built from the error map a request returns.