# `PgRest.Error`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/error.ex#L1)

A PostgREST-shaped error: HTTP status plus the `code`, `message`, `details` and
`hint` body fields. `headers` are response headers the error carries: `PgRest.Responder`
sends them without adding an authentication challenge. A headerless PgRest 401
defaults to `www-authenticate: Bearer`; `custom_headers?: true` preserves an empty
custom RAISE header map without that default.

## Differences from PostgREST

Changeset validation errors return 400/23514 with string details; PostgREST has
no application changesets.

# `t`

```elixir
@type t() :: %PgRest.Error{
  __exception__: true,
  code: String.t(),
  custom_headers?: boolean(),
  details: String.t() | [map()] | map() | nil,
  headers: [{String.t(), String.t()}],
  hint: String.t() | nil,
  message: String.t(),
  status: pos_integer()
}
```

# `aggregates_not_allowed`

```elixir
@spec aggregates_not_allowed() :: t()
```

`PGRST123`: an aggregate in `select=` while aggregates are disabled.

# `ambiguous_function`

```elixir
@spec ambiguous_function([String.t()]) :: t()
```

`PGRST203`: several functions match equally well (300). `candidates` are the `schema.name(arg =>
type, ...)` texts.

# `body_too_large`

```elixir
@spec body_too_large() :: t()
```

A body larger than `max_body_bytes` (413, Postgres `54000`).

# `check_violation`

```elixir
@spec check_violation(String.t(), :anon | :authenticated) :: t()
```

The write check failed: a written row is outside the resource's scope.

# `column_not_found`

```elixir
@spec column_not_found(String.t(), String.t()) :: t()
```

`PGRST204`: a payload or `columns=` name is not a column of the table.

# `connection_error`

```elixir
@spec connection_error() :: t()
```

`PGRST000`: PostgREST's 503 for a failed database connection; the driver's text is not sent.

# `function_not_found`

```elixir
@spec function_not_found(
  String.t(),
  String.t(),
  [String.t()],
  boolean(),
  String.t() | nil
) :: t()
```

`PGRST202`: no function matches the request (404). `keys` are the argument names the request
carries; `post?` adds the single-unnamed-parameter alternative to the details.

# `function_not_found_unnamed`

```elixir
@spec function_not_found_unnamed(String.t(), String.t(), String.t()) :: t()
```

`PGRST202` for a POST whose body is not JSON: only a function with one unnamed parameter of
`type` (`text`, `xml` or `bytea`) can take it, so no argument names are reported.

# `internal`

```elixir
@spec internal() :: t()
```

A generic 500 for a configuration problem; the cause is logged, never sent.

# `invalid_input`

```elixir
@spec invalid_input(String.t(), term()) :: t()
```

`22P02` for a filter value the column's type rejects, raised before the query reaches
Postgres (`PgRest.Ecto.Filter` casts values with the Ecto field type).

# `invalid_json`

```elixir
@spec invalid_json() :: t()
```

`PGRST102`: a request body that is not JSON.

# `invalid_path`

```elixir
@spec invalid_path() :: t()
```

`PGRST125`: the path is not `/<table>`.

# `invalid_payload`

```elixir
@spec invalid_payload(String.t()) :: t()
```

A 400 `23514` for a payload the resource's `changeset/3` (or the column types) rejects, as
a Postgres check violation would be. `details` is a string (`field: message; ...`), the
type `PostgrestError.details` has. PgRest only: PostgREST has no changesets.

# `invalid_preferences`

```elixir
@spec invalid_preferences([String.t()]) :: t()
```

`PGRST122`: `handling=strict` and `tokens` are not valid preferences.

# `invalid_range`

```elixir
@spec invalid_range(String.t()) :: t()
```

`PGRST103`: an unsatisfiable `Range` header or a negative `limit`.

# `invalid_schema`

```elixir
@spec invalid_schema(String.t(), [String.t()]) :: t()
```

`PGRST106`: an `Accept-Profile`/`Content-Profile` naming a schema that is not exposed (406).

# `invalid_token`

```elixir
@spec invalid_token(:decode | :claims, String.t(), String.t() | nil) :: t()
```

A 401 for a token that cannot be used: `PGRST301` (`:decode`, the token is malformed or its
signature is wrong) or `PGRST303` (`:claims`, a claim is invalid), with PostgREST's
`www-authenticate: Bearer error="invalid_token", error_description="<message>"`.

# `keys_must_match`

```elixir
@spec keys_must_match() :: t()
```

`PGRST102`: the rows of a bulk payload do not all have the same keys.

# `max_affected_exceeded`

```elixir
@spec max_affected_exceeded(non_neg_integer()) :: t()
```

`PGRST124`: `handling=strict` and the statement affected more than `max-affected` rows.

# `max_affected_rpc`

```elixir
@spec max_affected_rpc() :: t()
```

`PGRST128`: `handling=strict` with `max-affected` on a function that does not return a set.

# `no_relationship`

```elixir
@spec no_relationship(
  String.t(),
  String.t(),
  String.t(),
  String.t() | nil,
  String.t() | nil
) :: t()
```

`PGRST200`: no relationship from `parent` to the embed `name` in `schema`. `using` is the
request's embed hint, `hint` the suggestion (both may be `nil`).

# `not_embedded`

```elixir
@spec not_embedded(String.t()) :: t()
```

`PGRST108`: an embedded parameter names an embed that is not in the select.

# `not_objects`

```elixir
@spec not_objects() :: t()
```

`22023`: Postgres' own message when `columns=` lets a non-object element through.

# `offset_out_of_bounds`

```elixir
@spec offset_out_of_bounds(non_neg_integer(), non_neg_integer()) :: t()
```

`PGRST103`: the requested offset is past the known total.

# `parse_error`

```elixir
@spec parse_error(String.t(), String.t() | nil) :: t()
```

Builds a 400 `PGRST100` error, the code PostgREST uses for unparseable request text.

# `permission_denied`

```elixir
@spec permission_denied(:table | :function, String.t(), :anon | :authenticated) :: t()
```

`42501` for a table or a function the request may not use: `permission denied for table
<name>` or `permission denied for function <name>`, 401 for `:anon` and 403 otherwise (the
responder adds `www-authenticate: Bearer` to a 401).

# `pool_timeout`

```elixir
@spec pool_timeout() :: t()
```

`PGRST003`: no pool connection within the queue timeout (504).

# `put_filters_not_key`

```elixir
@spec put_filters_not_key() :: t()
```

`PGRST105`: a `PUT` whose filters are not exactly an `eq` on every primary key column (405).

# `put_limit_not_allowed`

```elixir
@spec put_limit_not_allowed() :: t()
```

`PGRST114`: `limit`/`offset` on a `PUT`.

# `put_payload_mismatch`

```elixir
@spec put_payload_mismatch() :: t()
```

`PGRST115`: no payload row of a `PUT` has the URL's primary key values.

# `related_order_not_possible`

```elixir
@spec related_order_not_possible(String.t(), String.t()) :: t()
```

`PGRST118`: `order=<relation>(<column>)` on an embed that is not to-one.

# `response_guc`

```elixir
@spec response_guc(:headers | :status) :: t()
```

Malformed response GUC (PGRST111 headers or PGRST112 status).

# `rpc_method_not_allowed`

```elixir
@spec rpc_method_not_allowed(String.t()) :: t()
```

`PGRST101`: an RPC request with a method other than GET, HEAD or POST (405).

# `schema_cache_not_ready`

```elixir
@spec schema_cache_not_ready() :: t()
```

`PGRST002`: the schema cache (the registry snapshot) is not available yet (503).

# `single_object`

```elixir
@spec single_object(non_neg_integer()) :: t()
```

`PGRST116`: a single object was requested and the result has `count` rows.

# `single_patch_object`

```elixir
@spec single_patch_object() :: t()
```

`PGRST102`: a `PATCH` payload that is an array of more than one object. A documented difference:
  PostgREST applies one of the objects arbitrarily.

# `single_put_object`

```elixir
@spec single_put_object() :: t()
```

`PGRST102`: a `PUT` payload with several objects that match the URL's key. A documented difference: PostgREST fails with a 500 `21000`.

# `spread_aggregate_not_implemented`

```elixir
@spec spread_aggregate_not_implemented(:to_many | :after_load) :: t()
```

`PGRST127`: an aggregate inside a spread that cannot hoist it: a one-to-many or many-to-many
spread (as PostgREST), or a spread over a resource with `after_load/2`, whose columns are merged
after the query (PgRest only).

# `table_denied`

```elixir
@spec table_denied(module() | map(), map() | :anon | :authenticated) :: t()
```

A table denial for a resource/config and request context or resolved role.

# `table_not_found`

```elixir
@spec table_not_found(String.t(), String.t(), String.t() | nil) :: t()
```

`PGRST205`: no such table in the schema cache.

# `to_body`

```elixir
@spec to_body(t()) :: %{required(String.t()) =&gt; String.t() | [map()] | map() | nil}
```

Returns the JSON body map. All four keys are always present; `nil` encodes as
JSON `null`.

# `token_required`

```elixir
@spec token_required() :: t()
```

PostgREST's 401 `PGRST302`: a request without a token where anonymous access is off.

# `too_many_rows`

```elixir
@spec too_many_rows() :: t()
```

A bulk payload with more rows than `max_bulk_rows` (413, Postgres `54000`).

# `undefined_column`

```elixir
@spec undefined_column(String.t()) :: t()
```

Postgres' own `42703` for a bare column name (an `on_conflict=` target).

# `unknown_column`

```elixir
@spec unknown_column(String.t(), String.t(), String.t() | nil) :: t()
```

PostgREST's `42703` for a column the table does not have.

# `unknown_role`

```elixir
@spec unknown_role(String.t()) :: t()
```

Postgres' `22023` for a role that does not exist, which PostgREST answers with a 401.

# `unreadable_body`

```elixir
@spec unreadable_body() :: t()
```

`PGRST102`: the request body could not be read.

# `unsupported_media`

```elixir
@spec unsupported_media([String.t()]) :: t()
```

`PGRST107`: none of the `Accept` media types is supported.

# `unsupported_method`

```elixir
@spec unsupported_method(String.t()) :: t()
```

`PGRST117`: the HTTP method is not supported.

# `where_required`

```elixir
@spec where_required(:update | :delete) :: t()
```

`21000`: a `PATCH` or `DELETE` without a filter while `require_filters_for_mutations` is on.
The status, code and message are those of Supabase's `pg-safeupdate`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
