Errors
One error shape for every endpoint, the codes and statuses, and what to do about each.
Every error has one shape:
{
"error": {
"code": "invalid_request",
"message": "The request body is invalid.",
"details": { "issues": [{ "path": "name", "message": "Required" }] }
}
}codeis stable and machine-readable. Branch on it, not on the message.messageis a sentence a person can act on. It is safe to show.detailsis there when there is more to say:issuesorfields(keyed by the path of the value) for validation errors,errorsandwarningsfor an invalid page or SiteSpec,problemswhen a workflow can't be published, andscopefor a missing scope.
Codes
| Code | Status | Means | Do |
|---|---|---|---|
bad_request | 400 | The request is malformed, for example not JSON. | Fix the request. |
confirmation_required | 400 | Deleting a site needs ?confirm=<siteId>. | Add the confirmation. |
unauthorized | 401 | No key, or the key isn't valid or was revoked. | Check the Authorization header. |
forbidden | 403 | The key or the account's role can't do this. | Use an owner's or admin's key. |
insufficient_scope | 403 | The key lacks the scope; details.scope names it. | Make a key with that scope. |
not_found | 404 | Not found, or not reachable with this key. | Check the id and the key's sites. |
conflict | 409 | It clashes with the current state: a taken hostname, a read-only synced collection, a request still in progress. | Read the message; retry later for in-progress. |
gone | 410 | Already deleted. | Nothing to do. |
precondition_failed | 412 | The page changed since you read it. | Read it again, then change it. See ETags. |
too_large | 413 | The file or body is too large. | Send less. |
unsupported_media_type | 415 | That file type isn't supported. | See Media for the types. |
invalid_request | 422 | The body is invalid; details says which values. | Fix the values named. |
invalid_spec | 422 | A page or SiteSpec has problems; details.errors lists them by path. | Fix each path. |
idempotency_mismatch | 422 | The Idempotency-Key was used before with a different body. | Use a new key. See Idempotency. |
rate_limited | 429 | Too many requests. | Wait Retry-After seconds. See Rate limits. |
internal_error | 500 | Something went wrong on our side. | Retry with backoff. |
Handle them
const res = await fetch(url, { headers });
if (!res.ok) {
const { error } = await res.json();
if (error.code === 'precondition_failed') {
// Someone changed the page: read it again and reapply your change.
} else if (error.code === 'rate_limited') {
await new Promise((r) => setTimeout(r, Number(res.headers.get('Retry-After')) * 1000));
} else {
throw new Error(`${res.status} ${error.code}: ${error.message}`);
}
}res = requests.get(url, headers=headers)
if not res.ok:
error = res.json()["error"]
if error["code"] == "precondition_failed":
pass # Someone changed the page: read it again and reapply your change.
elif error["code"] == "rate_limited":
time.sleep(int(res.headers["Retry-After"]))
else:
raise RuntimeError(f"{res.status_code} {error['code']}: {error['message']}")