Bier.PgError (bier v0.1.0)

Copy Markdown View Source

Translates a Postgrex.Error (a Postgres ServerError) into the PostgREST HTTP response: status code, JSON error envelope, extra response headers, and an optional custom reason phrase.

This mirrors PostgREST's pgErrorStatus (src/PostgREST/Error.hs) including the two special SQLSTATE families that give a function full control over the response:

  • PTxxx — the digits after PT are the HTTP status (non-numeric ⇒ 500).
  • PGRST — the MESSAGE is the JSON body envelope and the DETAIL is a JSON object {status, status_text, headers}. A malformed/missing MESSAGE/DETAIL becomes a PGRST121 parse error.

The returned shape is {status, body, headers, status_text} where body is a map with string keys "code" | "message" | "details" | "hint", headers is a list of {name, value} extra headers (e.g. the X-Header from a PGRST raise), and status_text is nil or a custom reason phrase.

Summary

Functions

Build the {status, body, headers, status_text} tuple for a Postgres server error. Returns nil if the error carries no Postgres postgres map (a connection/client error), so callers can fall back to a generic 500.

Types

result()

@type result() ::
  {status :: pos_integer(), body :: map(), headers :: [{binary(), binary()}],
   status_text :: binary() | nil}

Functions

translate(error)

@spec translate(Postgrex.Error.t()) :: result() | nil

Build the {status, body, headers, status_text} tuple for a Postgres server error. Returns nil if the error carries no Postgres postgres map (a connection/client error), so callers can fall back to a generic 500.