Skip to Content
Errors

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 timeout plugin's TimeoutError, 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 with TodoNotFound.

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:

ErrorWhenFields
NetworkErrorNo response: offline, DNS failure, connection refused, CORS rejection.method, url, cause
HttpErrorA response outside 200–299, 3xx included.method, url, status, headers, text, body
DecodeErrorA 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:

  • url is the request URL without its query string, so a token in the query doesn't end up in your logs.
  • HttpError.text is the raw body, and body is that body decoded. See Read the error body.
  • DecodeError.message is a readable summary of what went wrong, and issue holds the schema issue, the JSON SyntaxError or 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

On this page