Skip to Content
Errors

Errors by status

Turn an error status into your own domain error.

Often a status, or a status with a given body, means a specific error in your app: a 404 from GET /todos/:id is "todo not found", a 400 with "code": "INVALID_ROLE" is "invalid role", a 401 from anywhere is "session expired". statusErrors maps statuses to your own error schemas, and the call fails with your error instead of an HttpError. This page shows how to set up those maps and how Effetch picks the error for a response.

Choose where a map applies

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, for every call to it, which is where most of them live;
  • on a single call.

Map statuses to your errors

The following program gives a map in all three places, then catches the errors by their tags:

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

import {  } from "@monarcode/effetch";

class  extends .<>()("TodoNotFound", {
  : .,
}) {}

class  extends .<>()("InvalidRole", {
  : .("INVALID_ROLE"),
  : .,
}) {}

class  extends .<>()("RoleTaken", {
  : .("ROLE_TAKEN"),
  : .,
}) {}

class  extends .<>()(
  "SessionExpired",
  { : . },
) {}

const  = .({ : . });
const  = .({ : ., : . });
const  = .({ : ., : . });

// For every call
export class  extends .<>()("Api", {
  : "https://api.example.com",
  : ,
  : { 401:  },
}) {}

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

export const  = .(function* () {
  const  = yield* ;
  const  = yield* 
    .()({ : { : 1 } })
    .(.("TodoNotFound", () => .()));

  // For one call
  const  = yield* 
    .("/users/:id/roles", {
      : { : 1 },
      : .({ : . }),
      : { : "owner" },
      : ,
      : { 400: [, ] }, // tried in order
    })
    .(
      .({
        : () => .(.),
        : () => .(.),
      }),
    );
  return { ,  };
});

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

How a response is resolved

Effetch resolves a non-2xx response 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, then 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, its body decoded with error or defaultError.

With the maps from the program in Map statuses to your errors, responses fail as follows:

ResponseFails 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 bodyHttpError
401 from any endpointSessionExpired
500HttpError

See it in a full program

examples/status-errors.ts puts all of this in one program, and examples/app uses it across modules.

What's next

On this page