Skip to Content
Errors

Typed error bodies

Decode the API's error body with a schema.

APIs usually explain an error in its body, such as { "code": "not_found" }. This page shows how to give Effetch a schema for that body, so that HttpError.body is decoded and typed.

Give a schema for the error body

You can set the schema in two places:

  • defaultError, on the client, applies to every call;
  • error, on a call or an endpoint, replaces it.

Read the body

body is the decoded body, or undefined if it didn't match the schema. Without a schema, it's the parsed JSON. Reading it never fails the call, and the raw body is always on text.

Fetch.HttpError.With<B> is the type of an HttpError whose body is B, for your own function signatures.

What's next

On this page