A client call has two kinds of outcome: it receives an HTTP response, or it cannot produce a usable response. Handle those separately. An HTTP 404 is usually a resolved result; a network failure rejects the call.
The task client demonstrates status-based handling against the task contract. Run it with the service example.
Narrow by status before reading the body
createClient(contract, { baseUrl }) exposes one method per operation name. For a status declared with a schema, it validates the received body before returning it. Checking result.status selects the corresponding body type.
| Result | What the caller can assume |
|---|---|
| Declared status with a schema | The body passed that response schema |
| Declared empty response | The declared result type has no JSON body |
| Undeclared status | The body is unknown; narrow or validate it before use |
| Redirect status | Never returned; the call rejects |
| Rejected call | No usable result was returned; inspect the thrown error |
Do not cast an undeclared body to the success type. A reverse proxy can return an HTML error page, and an older server can have a different contract. The unknown branch is part of using HTTP reliably.
Distinguish failed calls
Client failures use HttpError from @skeld/core/http-error with status: null:
| Code | Meaning | Useful next step |
|---|---|---|
NETWORK |
Sending or receiving failed | Check connectivity and cancellation; decide whether retrying the operation is safe |
TIMEOUT |
The call did not finish within its time limit (30 s by default) | Check the service; raise timeoutMs only for an operation known to be slow |
INVALID_RESPONSE |
A declared response failed validation, JSON was invalid, or nesting exceeded the limit | Compare deployed contracts and inspect the server or proxy |
RESPONSE_TOO_LARGE |
The response exceeded the byte limit | Check whether the schema and operation are appropriately bounded |
REDIRECT_NOT_SUPPORTED |
The server answered with a redirect | Point baseUrl at the service itself, or answer without a redirect |
The client never follows a redirect. A 301, 302, 303, 307 or 308 answer rejects with REDIRECT_NOT_SUPPORTED, on Node and in a browser, whether or not the route declares that status. A route that declares one of those statuses therefore cannot be received through the typed client. The client cancels the redirect's body unread, and the error message names the method, origin and declared route path but never the Location value, which can carry a token. A 304 is unaffected, as is every other 3xx status: each is handled like any other response.
Response limits apply while reading decoded bytes, not only after buffering a whole body. By default, a bounded declared schema supplies its limit; an open or unbounded schema, or an undeclared status, uses 1 MiB. maxResponseBytes replaces every response limit for that client. Raising it is a deliberate resource decision, not a fix for a wrong response shape.
Bound how long a call waits
Every call has a time limit, 30,000 ms by default, counted from sending the request until the response body has been read and checked. Past it, the client aborts the request and the body read, closing the connection, and the call rejects with TIMEOUT. Its message is fixed text that holds no URL, header or body. Set timeoutMs in the client options to change the limit for every call, or pass it as a call's second argument to change it for that call only. false sets no limit; any other value must be an integer from 1 to 2,147,483,647, or createClient throws, and the call rejects, with TypeError. An operation that runs longer than 30 s needs a higher timeoutMs or false.
const created = await client.create({ body: { title: 'Read the contract' } }, { timeoutMs: 120_000 });A call also takes a signal. When it aborts, the call is cancelled the same way and rejects with the signal's own reason (an AbortError DOMException when none was given), never with an HttpError, so a caller's own cancellation stays distinguishable from a timeout and from a network failure. With both a signal and a limit, whichever comes first ends the call. Combining the two uses AbortSignal.any, which needs Node 20.3, Chrome 116, Firefox 124 or Safari 17.4.
The limit reaches the request through init.signal. A fetch passed in the client options must forward that signal for the limit to cancel anything: a wrapper that drops it leaves the call waiting for as long as its own transport does. Without the limit, Node's own fetch gives up only after 300 s without response headers, or 300 s between two pieces of the body, so a peer that keeps sending a few bytes now and then holds the call indefinitely.
The client does not automatically retry calls. A lost response does not prove a write failed: the server may have committed it before the connection broke. Establish idempotency in the application before retrying mutations.
Choose what the server exposes
Use a declared reply for a routine application outcome. Throw HttpError for an intentional HTTP error path. Its code, message, and permitted details are response content: write them for the caller, without tokens, database diagnostics, or private values.
By default, an arbitrary handler throw becomes the fixed response:
{ "status": 500, "code": "INTERNAL", "message": "Internal Server Error" }The thrown message is not copied into that body. A request that fails input validation instead receives HTTP 400 VALIDATION, with paths and descriptions rather than the rejected values. Failures inside classified data have coarser details to avoid revealing its structure.
The contract constrains normal handler replies. It does not turn author-written HttpError messages or low-level hook replies into schema-classified content. Review those explicit response paths as carefully as ordinary handlers.
Follow the request without exposing the failure
Use the response's x-request-id when investigating a problem. The engine owns this header; do not set it yourself. Engine request records use the same ID. Request IDs are correlation values, not proof of identity or authorization.
There may be no error record for a particular ID during flood suppression or delivery loss. Access records are opt-in and are not flood-suppressed, but they still use the bounded delivery queue. See logging delivery and suppression.
When catching an error and continuing, pass the error as a field to the logger instead of interpolating its message. The logging example does this for an expected SyntaxError. Default error fields contain recognized identifiers; messages, frames, and unlisted identifiers each require disclosure options. See errors in log records.
Next: organize a service. Reference: client and HttpError.