Errors
Every failed call produces one type, EndpointError. The throwing form throws it, .safe() returns it as result.error, and the builder's onError hook receives the same instance. This page lists what can go wrong and shows how to give error bodies a type.
Error kinds
error.kind says why the call failed. Switch on it first.
Every error also carries:
EndpointError extends NaviosError, which extends Error.
Typed error bodies
Without an errorSchema, every non-2xx reply is http-unknown and its body is unknown. Map statuses to schemas and those replies become kind http with a typed body.
Narrowing on kind and then status narrows body:
error.body is exactly what the schema produced. Builder adds nothing to it.
An error body that does not match its schema is reported as response-validation, because the server broke the contract.
In a catch block
A caught value is unknown, so the typing above is lost. Two guards bring it back.
isEndpointError(value)narrows toEndpointError.isHttpError(value, status?)narrows to kindhttp. Give it the endpoint'serrorSchematype and a status to also narrowbody.
If you find yourself writing more than one guard for the same call, .safe() is usually the shorter path. How it works compares the two forms.
Validation issues
Both validation kinds carry issues, in the same shape whichever schema library produced them. The issues of request-validation name the part of the arguments they belong to, 'urlParams', 'params' or 'data', and path starts inside it.
request-validation means your code passed bad input. response-validation means the server sent something the declaration does not allow; its body holds the raw payload so you can see what arrived.
Reporting with onError
onError is a telemetry hook on the builder. It is called once per failed call, with the same instance the caller receives, for both call forms.
It cannot change the outcome of a call. An exception thrown inside it is caught and logged.
error.toJSON() returns a plain object without cause, with headers flattened to a record and set-cookie left out. Its issues never contain the validated values. It is safe to pass to JSON.stringify, a logger, or across a server boundary.
Reacting to specific failures
Two patterns come up often enough to show.
Retry when the server says when:
Tell transient failures from permanent ones: