PgRest.Error exception (PgRest v0.2.0)

Copy Markdown View Source

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.

Summary

Functions

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

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

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

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

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

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

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.

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.

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

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).

PGRST102: a request body that is not JSON.

PGRST125: the path is not /<table>.

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.

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

PGRST103: an unsatisfiable Range header or a negative limit.

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

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>".

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

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

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

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).

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

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

PGRST103: the requested offset is past the known total.

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

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).

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

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

PGRST114: limit/offset on a PUT.

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

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

Malformed response GUC (PGRST111 headers or PGRST112 status).

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

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

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

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

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

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).

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

PGRST205: no such table in the schema cache.

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

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

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

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

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

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

PGRST102: the request body could not be read.

PGRST107: none of the Accept media types is supported.

PGRST117: the HTTP method is not supported.

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.

Types

t()

@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()
}

Functions

aggregates_not_allowed()

@spec aggregates_not_allowed() :: t()

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

ambiguous_function(candidates)

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

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

body_too_large()

@spec body_too_large() :: t()

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

check_violation(table, role \\ :anon)

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

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

column_not_found(table, column)

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

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

connection_error()

@spec connection_error() :: t()

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

function_not_found(schema, name, keys, post?, hint)

@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(schema, name, type)

@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()

@spec internal() :: t()

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

invalid_input(column, value)

@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()

@spec invalid_json() :: t()

PGRST102: a request body that is not JSON.

invalid_path()

@spec invalid_path() :: t()

PGRST125: the path is not /<table>.

invalid_payload(details)

@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(tokens)

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

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

invalid_range(details)

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

PGRST103: an unsatisfiable Range header or a negative limit.

invalid_schema(schema, exposed)

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

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

invalid_token(kind, message, details \\ nil)

@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()

@spec keys_must_match() :: t()

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

max_affected_exceeded(count)

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

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

max_affected_rpc()

@spec max_affected_rpc() :: t()

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

no_relationship(schema, parent, name, using \\ nil, hint \\ nil)

@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(name)

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

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

not_objects()

@spec not_objects() :: t()

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

offset_out_of_bounds(offset, total)

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

PGRST103: the requested offset is past the known total.

parse_error(message, details \\ nil)

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

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

permission_denied(kind, name, role \\ :anon)

@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()

@spec pool_timeout() :: t()

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

put_filters_not_key()

@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()

@spec put_limit_not_allowed() :: t()

PGRST114: limit/offset on a PUT.

put_payload_mismatch()

@spec put_payload_mismatch() :: t()

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

response_guc(atom)

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

Malformed response GUC (PGRST111 headers or PGRST112 status).

rpc_method_not_allowed(method)

@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()

@spec schema_cache_not_ready() :: t()

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

single_object(count)

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

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

single_patch_object()

@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()

@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(kind)

@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(config, role)

@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(schema, table, hint \\ nil)

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

PGRST205: no such table in the schema cache.

to_body(error)

@spec to_body(t()) :: %{required(String.t()) => String.t() | [map()] | map() | nil}

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

token_required()

@spec token_required() :: t()

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

too_many_rows()

@spec too_many_rows() :: t()

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

undefined_column(column)

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

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

unknown_column(table_alias, column, hint \\ nil)

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

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

unknown_role(role)

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

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

unreadable_body()

@spec unreadable_body() :: t()

PGRST102: the request body could not be read.

unsupported_media(types)

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

PGRST107: none of the Accept media types is supported.

unsupported_method(method)

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

PGRST117: the HTTP method is not supported.

where_required(kind)

@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.