Skip to content
CirrusDocs

Guides

Errors

The error envelope, every status the API answers with, and the codes you can act on.

The error envelope#

Every error, from every operation, has the same shape. A successful answer is { "data": … }; a failure is { "error": … } with a matching HTTP status.

A validation error
{
  "error": {
    "code": "validation_failed",
    "message": "Some fields are invalid.",
    "request_id": "4f1c2b8e9a3d47e0b6c5a1d2e3f40718",
    "details": [
      { "field": "to", "message": "Contains an invalid email address." }
    ]
  }
}
Fields of an error
FieldMeaning
codeA stable identifier for programs, such as validation_failed. Switch on this, never on the message.
messageA sentence that is safe to show a person. Its wording can change.
request_idIdentifies the request in our logs. Quote it when you ask for help. It is also in the X-Request-Id response header.
detailsA list of { field, message }, one per invalid field. Also one per refused recipient of a marketing or promotional email.
resource_idThe existing resource a conflict is about. Left out otherwise.

You can send your own X-Request-Id request header (8 to 64 letters, digits, dots, hyphens or underscores) to have it used as the request ID, which makes it easy to match a call in your logs to ours. Anything else is replaced with one we make up.

Statuses#

HTTP statuses and what to do
StatusMeaningRetry?
400The body is not one JSON object, or has a field the endpoint does not accept (invalid_json). Unknown fields are rejected, not ignored.No. Fix the request.
401No key, or an unknown, revoked or expired key (unauthenticated, invalid_api_key).No. See Authentication.
403The key lacks the scope, the organisation is suspended or closed, or the send was refused by a sending rule.No, until the cause is fixed.
404No such resource, or no such route (not_found). An ID that is not a valid UUID is also a 404.No.
405The method is not allowed on that route (method_not_allowed).No.
409A conflict, such as domain_exists, domain_claimed or webhook_limit_reached.No.
413The body is larger than 1 MB (body_too_large).No.
422Well-formed but not acceptable: validation_failed, idempotency_key_reused and the rules of the marketing streams.No. Fix the request.
429Too many requests (rate_limited). Wait Retry-After seconds.Yes, after waiting.
5xxSomething went wrong on our side (internal_error).Yes, with backoff. Safe for reads and for sends with an idempotency key.

Codes you can act on#

Authentication and account#

Authentication and account error codes
CodeStatusMeaning
unauthenticated401No API key was sent.
invalid_api_key401The key is unknown, revoked or expired.
insufficient_scope403The key does not hold the scope this operation needs.
account_suspended403The organisation is suspended; none of its keys work until that is lifted.
account_closed403The organisation has been closed; none of its keys will work again.

Sending#

Sending error codes
CodeStatusMeaning
validation_failed422details names each invalid field. Idempotency-Key is the field for a bad key.
idempotency_key_reused422The key was used before with a different body.
stream_disabled403Sending on this stream has been turned off for the organisation.
stream_not_allowed403The key may only send transactional email (it lacks campaign:write).
domain_not_verified403The From domain is not registered or not verified.
domain_disabled403The From domain has been disabled.
sender_not_authorised403The From address may not be used.
template_not_published422template_id names a template with no published version.
template_category_mismatch422stream is not the template's category.
single_recipient_required422A marketing or promotional email has more than one recipient.
postal_address_not_set422Your organisation has not set its postal address.
business_identification_missing422A body part that places its own footer does not show the postal address.

A marketing or promotional recipient refused for its own reasons answers 403 with the reason as the code, such as recipient_unsubscribed. They are listed in Marketing and promotional email.

Domains and webhooks#

  • domain_exists (409): you have already registered this domain.
  • domain_claimed (409): another organisation has already verified it.
  • webhook_limit_reached (409): your organisation already has the maximum number of endpoints, 20.

Errors in the libraries#

The libraries turn each of these into an exception of its own kind, by status, with the code, the message, the request ID, the field details and, for a 429, the wait. A problem with no HTTP answer at all (the network, a timeout) is a connection error, a different kind. An answer that is not our envelope, such as an HTML page from a proxy, becomes the code unexpected_response with the status; the libraries never fail with a JSON parse error.

import { APIError, RateLimitError, ValidationError } from "@cirrus-mail/cirrus";

try {
  await cirrus.emails.send(message);
} catch (error) {
  if (error instanceof ValidationError) {
    console.log(error.details); // [{ field, message }, ...]
  } else if (error instanceof RateLimitError) {
    console.log("try again in", error.retryAfter, "seconds");
  } else if (error instanceof APIError) {
    console.log(error.status, error.code, error.requestId);
  } else {
    throw error; // a connection problem, or a bug
  }
}