Query errors
How a query's errors stay typed, from the call to the component.
A query's error type is part of its type: Query<A, E>. A failed query's
result is a Failure with the full Cause, so you can tell three things
apart:
The examples import todos.ts from Getting started.
- the query's errors, typed as
E: an EffetchHttpErrororNetworkError, your own errors by status or the errors your Effect fails with; - defects, such as a bug that throws;
- interruptions, such as a fetch replaced by a refetch.
In a component, match on the typed errors by their _tag with
AsyncResult.builder's onErrorTag, or by kind with
AsyncResult.matchWithError (Read a query). In Effect,
Query.fetch fails with the query's error, so Effect.catchTag handles
it (Use the cache from Effect).
The client's defaultError types the body of an HttpError. In
1. Describe your API, ApiClient declares
ApiError, so every endpoint's HttpError is
HttpError.With<{ message: string }>. An endpoint with an error schema
of its own uses that one instead (ErrorSchema is that choice, as a
type), and todo.error holds the schema an endpoint query decodes its
error bodies with.
Retry with a Schedule. A query doesn't retry by default, where
TanStack Query retries three times: Effetch has a retry plugin, and a
query shouldn't retry what the client already retried. To retry at the
query, give it a retry schedule:
import * as from "effect/Schedule";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "./todos.js";
export const = .(
.("GET /stats", {
: .({ : ., : . }),
}),
// Two more tries before the failure shows.
{ : .(2) },
);The schedule gets each failure, and defects aren't retried. The client's
queries.defaults.retry sets one for every query
(Defaults and app-wide hooks), and the client's onFailure hook runs once
per fetch, after retry.