Error

View as Markdown

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 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 instead.

NameTypeDescription
errorMessageString?Message that describes the failure. Defaults to null.
errorCodeint?Contentstack error code. Defaults to null.
errorsMap?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 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 for the full error code list.

Class-Level Properties Table

PropertyTypeAccessDefaultDescription
errorMessageString?Read-onlynullThe message of the error, from the error_message key.
errorCodeint?Read-onlynullThe Contentstack error code, from the error_code key.
errorsMap?Read-onlynullThe details for each field that failed, from the errors key.

Method Index

Method NameReturnsDescription
fromJsonA new error that holds the values of the error map.Builds an Error from the map a request returns.
toJsonThe 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.

NameTypeDescription
json (required)Map<String, dynamic>

Error body that the request returned.

Default: Not applicable

Returns: Error. A new error that holds the values of the error map.

Validation

  • Throws 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.

Behavior

  • Client-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).

Example

Basic usage: build an Error from a rejected request

import '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 details

import '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'));
  }
}

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.

Validation

  • There is no client-side validation. toJson cannot fail.

Behavior

  • Client-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.

Example

Basic usage: log an error as JSON

import '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()));
}