Errors, request ids and retries
The error body
Section titled “The error body”Every failed call answers with a status code and a JSON body:
{ "error": "\"grumpy\" is not a valid temperament", "code": "invalid_value", "request_id": "req_9b1e04f3c2a7d815", "field": "temperament", "allowed": ["responsive", "busy", "procedural", "skeptical", "flaky", "difficult", "ghost"]}| Field | Meaning |
|---|---|
error | What went wrong, in words, for people. It may change; do not match on it. |
code | A stable name a program can act on (below). |
request_id | The request’s id, also in the X-Request-Id header. Quote it when you ask for help. |
field | The parameter or field at fault, where there is one. |
allowed | The values the field allows, when a value was not one of them. See the settings reference. |
near_misses, matched, waited_ms, messages | On a wait that timed out: see the wait API. |
message | On unexpected_email: the email that arrived. |
feature | On not_in_plan: the plan feature it needs. |
| Code | Status | Meaning |
|---|---|---|
bad_request | 400 | The request is wrong: bad JSON, a missing field. |
invalid_value | 400 | A value is not one the field allows; see field and allowed. |
invalid_regex | 400 | A wait’s subject_regex or body_regex is not a valid regular expression. |
unauthorized | 401 | No key, or a key that is not valid. |
not_in_plan | 402 | Your plan does not include this feature. |
forbidden | 403 | Your role cannot do this, the key belongs to another universe, or the organization is suspended. |
not_found | 404 | No such mailbox, message, persona, case, job or share. |
method_not_allowed | 405 | Wrong method. |
wait_timeout | 408 | A wait timed out. |
timeout | 408 | Another request timed out. |
unexpected_email | 409 | GET /api/wait/none: a matching email arrived. |
conflict | 409 | A conflict, such as a person who decided not to reply to a fast reply wait. |
too_large | 413 | The request body is too large. |
idempotency_mismatch | 422 | The Idempotency-Key was used for a different request in the last 24 hours. |
unprocessable | 422 | The request is well formed but cannot be done. |
rate_limited | 429 | Too many requests; wait as Retry-After says. |
internal_error | 500 | Something failed on the server. |
bad_gateway, unavailable | 502, 503 | The server could not be reached; try again. |
A plan’s numeric limits (people, logins, streams) are refused with 400 and a message such as “the Starter plan includes 250 people; upgrade for more”.
Request ids
Section titled “Request ids”Every answer, success or failure, carries an X-Request-Id header, and every error body repeats it as request_id. Send your own X-Request-Id (letters, digits and . _ : -, up to 64) to tie simmail’s logs to your test run; otherwise simmail makes one, such as req_40d5c947215616d7.
curl -s -i https://c1.simmail.dev/api/me -H "Authorization: Bearer smk_your_key" -H "X-Request-Id: ci-4821-signup-test"Safe retries with Idempotency-Key
Section titled “Safe retries with Idempotency-Key”A network blip during POST /api/send or POST /api/inject leaves you unsure whether the email went. Send an Idempotency-Key header (any string up to 255 characters, unique per email) and retry freely:
- The same key with the same request within 24 hours gets the first answer back, with the header
Idempotent-Replayed: true, and nothing is sent again. - The same key with a different request answers
422(idempotency_mismatch). - Server errors (
5xx) are not remembered, so a retry after one runs again. - Two requests with the same key at once run one after the other; the second gets the first’s answer.
curl -s -X POST https://c1.simmail.dev/api/send \ -H "Authorization: Bearer smk_your_key" -H "Content-Type: application/json" \ -H "Idempotency-Key: signup-test-4821-welcome" \ -d '{"from": "[email protected]", "to": ["[email protected]"], "subject": "Welcome", "text": "Hi Dana"}'The SDKs send a fresh key with every send and inject and retry on network errors, 502 and 503, so a retried call can never send twice. Pass your own with { idempotencyKey: "…" } (idempotency_key= in Python), or false (False) to send none.
Errors in the SDKs
Section titled “Errors in the SDKs”Every error is a SimmailError with status, code, requestId (request_id), and the fields above (field, allowed, nearMisses / near_misses, and so on). Its class says what happened, so a test can catch exactly what it expects:
| Class | Codes |
|---|---|
ValidationError | bad_request, invalid_value, invalid_regex (and any other 400) |
AuthError | unauthorized |
PlanLimitError | not_in_plan |
ForbiddenError | forbidden |
NotFoundError | not_found |
WaitTimeoutError | wait_timeout, timeout |
ConflictError | conflict |
UnexpectedEmailError (a ConflictError) | unexpected_email |
IdempotencyError | idempotency_mismatch |
RateLimitedError | rate_limited, with retryAfter (retry_after) |
SimmailError | anything else |
import { WaitTimeoutError, ValidationError } from "@plexuslabs/simmail";
try { await mail.waitForEmail({ to: addr, subject: "verify", timeout: "20s" });} catch (e) { if (e instanceof WaitTimeoutError) console.log(e.nearMisses, e.requestId); else if (e instanceof ValidationError) console.log(e.field, e.allowed); else throw e;}from simmail import WaitTimeoutError, ValidationError
try: mail.wait_for_email(addr, subject="verify", timeout=20)except WaitTimeoutError as e: print(e.near_misses, e.request_id)except ValidationError as e: print(e.field, e.allowed)