# Errors by status (https://effetch.monarcode.dev/docs/errors/status-errors)

An error status often means something specific in your app. A 404 from
`GET /todos/:id` means "todo not found", a 400 with
`"code": "INVALID_ROLE"` means "invalid role", and a 401 with a message
from any endpoint means "session expired". Catching each of these as an
`HttpError` and checking its status at every call gets repetitive.
`statusErrors` maps statuses to error schemas of your own, so the call fails
with your error instead of an `HttpError`, and the error's tag says what
went wrong. This page shows how to set up those maps, and how Effetch picks
the error for a response.

## Map statuses to your errors

A map's keys are statuses, and each value is an error schema, or a list of
them to try in order. The following program gives a map on the client, on
an endpoint and on one call, then catches the errors by their tags:

```ts
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import { Fetch } from "@monarcode/effetch";

class TodoNotFound extends Schema.TaggedError<TodoNotFound>()("TodoNotFound", {
  message: Schema.String,
}) {}

class InvalidRole extends Schema.TaggedError<InvalidRole>()("InvalidRole", {
  code: Schema.Literal("INVALID_ROLE"),
  message: Schema.String,
}) {}

class RoleTaken extends Schema.TaggedError<RoleTaken>()("RoleTaken", {
  code: Schema.Literal("ROLE_TAKEN"),
  message: Schema.String,
}) {}

class SessionExpired extends Schema.TaggedError<SessionExpired>()(
  "SessionExpired",
  { message: Schema.String },
) {}

const ApiError = Schema.Struct({ code: Schema.String });
const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });
const User = Schema.Struct({ id: Schema.Finite, role: Schema.String });

// For every call
export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  defaultError: ApiError,
  statusErrors: { 401: SessionExpired },
}) {}

// For every call to an endpoint (see Endpoints)
export const getTodo = Fetch.endpoint("GET /todos/:id", {
  output: Todo,
  statusErrors: { 404: TodoNotFound },
});

export const program = Effect.gen(function* () {
  const api = yield* Api;
  const todo = yield* api
    .bind(getTodo)({ params: { id: 1 } })
    .pipe(Effect.catchTag("TodoNotFound", () => Effect.succeed(undefined)));

  // For one call
  const user = yield* api
    .post("/users/:id/roles", {
      params: { id: 1 },
      input: Schema.Struct({ role: Schema.String }),
      body: { role: "owner" },
      output: User,
      statusErrors: { 400: [InvalidRole, RoleTaken] }, // tried in order
    })
    .pipe(
      Effect.catchTags({
        InvalidRole: (error) => Effect.fail(error.message),
        RoleTaken: (error) => Effect.fail(error.message),
      }),
    );
  return { todo, user };
});
```

Before the `catchTags`, the role call fails with
`InvalidRole | RoleTaken | SessionExpired | HttpError.With<{ readonly code: string }> | NetworkError | DecodeError`.
`HttpError` stays in the type, because a response can always fail to match
your schemas.

## Choose where a map applies

As the program shows, you can give a map in three places, and they add up:

- **On the client,** for every call, such as a 401 from anywhere.
- **On an [endpoint](https://effetch.monarcode.dev/docs/endpoints),** for every call to it. Most maps live
  here, next to the route they describe.
- **On a single client call,** such as `api.get` or `api.post`.

An endpoint call takes no map of its own. It uses the endpoint's map and the
client's, so to add a status for one endpoint, put it on the endpoint's
definition.

## How a response is resolved

When a response comes back with a non-2xx status, Effetch picks the error in
the following order:

1. If the body is empty or isn't JSON, it's an `HttpError`.
2. Otherwise, the schemas listed for its status are tried in order: the
   call's or the endpoint's, then the client's. The first one that decodes
   the body is the error.
3. If none does, it's the `HttpError` you'd get without `statusErrors`, with
   its `body` decoded by `error` or `defaultError`.

With the maps from the program in
[Map statuses to your errors](https://effetch.monarcode.dev/docs/errors/status-errors#map-statuses-to-your-errors), responses fail
as follows:

| Response                                         | Fails with                   |
| ------------------------------------------------ | ---------------------------- |
| 400 `{ "code": "INVALID_ROLE", "message": "…" }` | `InvalidRole`                |
| 400 `{ "code": "ROLE_TAKEN", "message": "…" }`   | `RoleTaken`                  |
| 400 `{ "code": "MISSING_FIELD" }`                | `HttpError` with `body.code` |
| 400 with an empty or non-JSON body               | `HttpError`                  |
| 401 `{ "message": "…" }` from any endpoint       | `SessionExpired`             |
| 401 with an empty body                           | `HttpError`                  |
| 500                                              | `HttpError`                  |

The empty 401 is the case to watch for. Some servers send a 401 or a 404
with no body at all, and `statusErrors` can't map a body that isn't there.
For a status whose body may be empty, catch the `HttpError` with
[`Fetch.catchStatus`](https://effetch.monarcode.dev/docs/errors/catching#catch-a-status), alongside your
error's tag.

**Details: tags, renamed fields, unions and types**

- **The `_tag`.** Backends don't send one, so when a schema has a literal
  `_tag`, as `Schema.TaggedError` classes do, Effetch adds it to the body
  before decoding. A body that has its own `_tag` is decoded as it is.
- **Renamed fields.** To read `error_code` into `code`, use
  `Schema.encodeKeys`:
  `InvalidRole.pipe(Schema.encodeKeys({ code: "error_code" }))`.
- **Lists, not unions.** A `Schema.Union` is decoded as it is, without a
  `_tag`, so list its members instead.
- **2xx entries never apply,** since a 2xx is decoded with `output`.
- **Plugins get the response first.** They run before any of this, so
  `retry` still retries by status. `onError` receives your error, and
  `api.httpClient` stays raw.
- **Maps are copied when they're given,** so changing one afterwards changes
  nothing. `api.statusErrors` and a `Fetch.endpoint` definition's
  `statusErrors` are those copies, frozen. An endpoint made with
  `api.endpoint` or `api.bind` has `statusErrors` that join its own with the
  client's, each status listing its schemas in the order they're tried.
- **Types.** `Fetch.StatusError<M>` is the union of the errors in a map `M`,
  and `Fetch.StatusErrorSchema<M>` the union of its schemas, for your own
  function signatures.
- **A client's type includes its map:** `Fetch.Client<Ps, EB, SE>`. A client
  with a map can't be passed where a plain `Fetch.Client` is expected, since
  that would hide its errors.

## Test a map

To check that a status turns into your error, make a fake backend answer
with it. `FetchTest.fail(getTodo, 401, new SessionExpired({ message: "…" }))`
sends the error as JSON, and the client decodes it back into
`SessionExpired`. When the endpoint has no `error` or `statusErrors` schema
of its own, this works with a map that's only on the client, so you don't
need to repeat it on the endpoint. See
[Test errors and statuses](https://effetch.monarcode.dev/docs/testing/errors).

## See it in a full program

[`examples/status-errors.ts`](https://github.com/monarcode/effetch/blob/main/examples/status-errors.ts)
puts all of this in one program, and
[`examples/app`](https://github.com/monarcode/effetch/tree/main/examples/app)
uses it across modules.

## What's next

- [Catch errors](https://effetch.monarcode.dev/docs/errors/catching): catch your errors, and the statuses
  you didn't map.
- [Define endpoints](https://effetch.monarcode.dev/docs/endpoints/defining): put each endpoint's
  `statusErrors` on its definition.
- [Test errors and statuses](https://effetch.monarcode.dev/docs/testing/errors): make a fake backend
  answer with an error status.