Errors
An error has an HTTP status that tells you the class of problem and a stable code that tells you which one. Write your handling against the code, never the sentence.
Error shapes#
Most errors come from the route itself and are application/problem+json:
{
"title": "Not Found",
"status": 404,
"detail": "That teaching is not in this church.",
"code": "not_found"
}A few checks run before any route: authentication, rate limits, a missing Idempotency-Key. They answer application/json in the shape of the success envelope:
{
"status": false,
"message": "That API key is not valid.",
"code": "invalid_api_key"
}Both carry code, and every error the API returns is JSON with a code, including unknown routes, unsupported methods and content types, validation failures and server errors. The sentence (detail or message) is written for people and its wording can improve, so show it if you like, but do not match on it.
Validation errors#
When a request body or a parameter does not fit the schema, the code is validation_failed, detail is validation failed, and an errors array lists each problem with the location of the value:
{
"title": "Unprocessable Entity",
"status": 422,
"detail": "validation failed",
"errors": [
{
"message": "expected length >= 2",
"location": "query.q",
"value": "a"
}
]
}Unknown routes#
A path that does not exist answers 404 not_found and a method a route does not support answers 405 method_not_allowed with an Allow header. Both mean your URL or method is wrong.
Handling errors#
- 400, 403, 404, 409, 422: the request will not succeed as sent. Fix it, or show the person a message. Do not retry the same request.
- 401: the key is missing, wrong, revoked or expired. Stop and alert someone.
- 429: rate limits are worth retrying after
Retry-After. Spend and teaching caps are not: they clear when the period rolls over or someone raises the cap. - 500 and 503: retry with exponential backoff and the same
Idempotency-Key.
try {
await lectern("/teachings/tch_does_not_exist");
} catch (error) {
if (error instanceof LecternError) {
console.log(error.status, error.code, error.message, error.requestId);
} else {
throw error;
}
}Keep the request id (X-Request-Id) in your logs next to the error. See Rate limits for a retry helper that follows these rules.
Codes from checks before the route#
| Code | HTTP | Meaning and fix |
|---|---|---|
credential_conflict | 400 | The request had both an Authorization header and a browser session cookie. Send only the key. |
key_in_url | 400 | The key was in the query string. Move it to the Authorization header and revoke the leaked key. |
invalid_api_key | 401 | No key, a malformed key, or a key that is revoked, expired or unknown. The response carries WWW-Authenticate. |
ip_not_allowed | 403 | The key has an IP allowlist and the request came from another address. Call from an allowed address or edit the list. |
idempotency_key_required | 422 | A route that starts work needs an Idempotency-Key header. Add one. |
rate_limited | 429 | This key went over its requests per minute. Wait Retry-After seconds. |
org_rate_limited | 429 | The church as a whole went over its ceiling across all keys. Wait Retry-After seconds. |
Reusing an Idempotency-Key with a different body answers 422 idempotency_key_reused. A request body that is not JSON answers 415 unsupported_media_type, and one that is too large answers 413 payload_too_large.
Codes from routes#
| Code | HTTP | Meaning and fix |
|---|---|---|
bad_request | 400 | The request could not be read, for example malformed JSON. Fix the body. |
invalid_id | 400 | An id in the path or body is not a valid Lectern id. Pass ids exactly as returned. |
invalid_include | 400 | include on a teaching had an unknown value. Use transcript or latest_note. |
insufficient_scope | 403 | The key lacks the scope the route needs. The detail names it. Create a key with the scope. |
forbidden | 403 | The key is not allowed to do that. |
plan_hours_exhausted | 403 | The church has used the hours included in its plan. A person at the church needs to act in Lectern. |
not_found | 404 | Nothing with that id in this church. A key never sees another church, so a wrong slug is also 404. |
note_not_found | 404 | The teaching has no note at all. |
note_not_approved | 404 | A note exists but nobody has approved it yet. Wait for the note.approved event. |
duplicate_teaching | 409 | That video is already in the church. |
transcript_not_ready | 409 | The teaching is still being prepared. Wait until status is done. |
answer_pending | 409 | The last question in this conversation is still being answered. Wait for it, then ask again. |
webhook_not_active | 409 | The endpoint is paused or disabled. Turn it back on with PATCH. |
conflict | 409 | The request conflicts with the current state. |
precondition_failed | 412 | A precondition on the request did not hold. |
unsupported_url | 422 | Only YouTube video links are accepted by POST /teachings. Upload other recordings. |
invalid_question | 422 | A question is 2 to 500 characters. |
invalid_query | 422 | A search is 2 to 300 characters. |
webhook_url_not_allowed | 422 | Webhooks need a public https address on port 443. Localhost and private networks are refused. |
validation_failed | 422 | The body or a parameter failed validation, or an upload failed a check. The errors array or the detail says which. |
idempotency_key_reused | 422 | The Idempotency-Key was already used with a different body. Use a new key. |
method_not_allowed | 405 | That route does not support the method. See the Allow header. |
unsupported_media_type | 415 | The body is not JSON. Send Content-Type: application/json. |
payload_too_large | 413 | The request body is too large. |
key_daily_cap_reached | 429 | This key reached its daily spend cap. |
key_monthly_cap_reached | 429 | This key reached its monthly spend cap. |
org_api_cap_reached | 429 | The church reached its monthly API spend cap. |
monthly_teaching_limit_reached | 429 | This key reached its monthly limit of submitted teachings. |
signing_unavailable | 503 | Webhook signing is not available on this server. |
unavailable | 503 | The service is temporarily unavailable. Retry with backoff. |
internal | 500 | Something went wrong on our side. Retry with backoff and quote the request id if it persists. |
Upload codes#
| Code | HTTP | Meaning and fix |
|---|---|---|
unsupported_type | 422 | The file extension is not accepted. See media_extensions and text_extensions in the policy. |
file_too_large | 422 | The file is over the limit. Save just the audio, which is much smaller. |
recording_too_long | 422 | The recording is longer than max_hours. |
duration_unknown | 422 | The length of the recording could not be read. |
size_mismatch | 422 | The bytes you sent do not add up to size_bytes. |
wrong_file_type | 422 | The file’s contents do not match its extension. |
empty_transcript | 422 | The transcript file has no text. |
parts_missing | 409 | Not every part has arrived. Read the upload and send the rest. |
upload_closed | 409 | The upload is finished, cancelled or expired. |
still_finishing | 409 | A previous call to complete is still running. |
upload_quota_reached | 403 | The church reached its upload storage quota. |
ai_budget_reached | 403 | The church reached its AI budget. |
too_many_uploads | 429 | Too many uploads are in progress. Finish or cancel one. |
storage_unavailable | 502 | Storage could not be reached. Retry. |
uploads_unavailable | 503 | Uploads are not set up on this server. |
If your code meets a code it does not know, handle it by HTTP status: 4xx is yours to fix, 5xx is worth a retry.