Call errors
Learn what a call can fail with and how each error is typed.
A request can go wrong in more ways than one. The network can drop, the server can answer with a 404, or the body can come back in a shape you didn't expect. With Effetch, each of these is a separate tagged error, and every call's type lists the ones it can fail with. TypeScript then tracks which errors you've handled and which are left.
This page describes those errors and how they fit together. The rest of the section shows how to catch them, read the API's error body, and turn error statuses into errors of your own.
How a call fails
A call fails with one of three errors, each with its own _tag:
NetworkError: the request got no response at all.HttpError: the server answered, but with a status outside 200–299.DecodeError: the server answered with a 2xx, but its body couldn't be read or decoded.
Here's how they show up in a call's type:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
// Fails with NetworkError, HttpError or DecodeError
export const = .("/todos/:id", {
: { : 1 },
: ,
});Two things can add to that list:
- Plugins. A plugin can fail a call with an error of its own, such as
the
timeoutplugin'sTimeoutError, and that error joins the call's type. - Your own errors. With
statusErrors, an error status becomes an error you define, such as a 404 that fails withTodoNotFound.
The errors and their fields
Every error carries the request's method and url, so a log line says
which call failed. The following table lists when each error happens and
the fields it carries:
| Error | When | Fields |
|---|---|---|
NetworkError | No response: offline, DNS failure, connection refused, CORS rejection. | method, url, cause |
HttpError | A response outside 200–299, 3xx included. | method, url, status, headers, text, body |
DecodeError | A 2xx body that couldn't be read, isn't JSON or doesn't match output, on a call with output. Or a stream that fails while it's read. | method, url, status, issue, message |
TimeoutError (plugin) | The timeout plugin's limit passed before the response arrived, or the retry plugin's budget ran out during an attempt. | method, url, duration |
A stream (api.sse, api.ndjson or a stream endpoint) fails with
DecodeError with or without output: when a value doesn't decode, when
the body breaks off, or when an SSE response's Content-Type isn't
text/event-stream. For how a stream reads its body, see
Streaming responses.
A few of these fields are worth knowing before you handle them:
urlis the request URL without its query string, so a token in the query doesn't end up in your logs.HttpError.textis the raw body, andbodyis that body decoded. See Read the error body.DecodeError.messageis a readable summary of what went wrong, andissueholds the schema issue, the JSONSyntaxErroror the read error behind it.
Errors and defects
The errors in the table are things your app can recover from: show a
message, retry, or fall back to a cached value. A mistake in your own code,
such as a missing path parameter, is different. It makes the Effect die as
a defect (a failure that isn't in the error type, which you don't catch in
normal code), so the bug surfaces instead of passing as an HttpError.
Logging and defects lists those
mistakes.
What's next
- Catch errors: handle an error by its tag or its status, and read the API's error body.
- Errors by status: turn an error status into your own domain error.
- Logging and defects: see what stays out of logged errors, and which mistakes are defects.
- Test errors and statuses: make a fake backend fail, to test your handlers.