Anomaly-based error handling for the Supabase Clojure SDK.
Errors are represented as plain maps following the cognitect/anomalies convention. This avoids tagged tuples and exceptions by default, matching the data-driven philosophy of libraries like cognitect/aws-api.
The SDK maps HTTP status codes and domain errors to these categories:
:cognitect.anomalies/incorrect — bad request, validation failure (4xx client errors):cognitect.anomalies/forbidden — authentication/authorization failure (401, 403):cognitect.anomalies/not-found — resource not found (404):cognitect.anomalies/conflict — resource already exists (409):cognitect.anomalies/busy — rate limited, resource locked (423, 429):cognitect.anomalies/unavailable — server error, service unavailable (5xx):cognitect.anomalies/fault — unexpected server-side failureAn anomaly map always contains :cognitect.anomalies/category and may include:
:cognitect.anomalies/message — human-readable error description:supabase/service — originating service (:auth, :storage, etc.):supabase/code — semantic error code keyword (e.g. :not-found):http/status — original HTTP status code:http/body — response body (parsed or raw):http/headers — response headers(require '[supabase.core.error :as error])
;; Check if a result is an error
(error/anomaly? result)
;; Create an anomaly from an HTTP response
(error/from-http-response 404 {:message "Not Found"} :storage)
;; Create a domain-specific anomaly
(error/anomaly :cognitect.anomalies/incorrect
{:supabase/service :auth
:cognitect.anomalies/message "Invalid credentials"})
Anomaly-based error handling for the Supabase Clojure SDK.
Errors are represented as plain maps following the cognitect/anomalies convention.
This avoids tagged tuples and exceptions by default, matching the data-driven
philosophy of libraries like cognitect/aws-api.
## Anomaly Categories
The SDK maps HTTP status codes and domain errors to these categories:
- `:cognitect.anomalies/incorrect` — bad request, validation failure (4xx client errors)
- `:cognitect.anomalies/forbidden` — authentication/authorization failure (401, 403)
- `:cognitect.anomalies/not-found` — resource not found (404)
- `:cognitect.anomalies/conflict` — resource already exists (409)
- `:cognitect.anomalies/busy` — rate limited, resource locked (423, 429)
- `:cognitect.anomalies/unavailable` — server error, service unavailable (5xx)
- `:cognitect.anomalies/fault` — unexpected server-side failure
## Structure
An anomaly map always contains `:cognitect.anomalies/category` and may include:
- `:cognitect.anomalies/message` — human-readable error description
- `:supabase/service` — originating service (`:auth`, `:storage`, etc.)
- `:supabase/code` — semantic error code keyword (e.g. `:not-found`)
- `:http/status` — original HTTP status code
- `:http/body` — response body (parsed or raw)
- `:http/headers` — response headers
## Usage
(require '[supabase.core.error :as error])
;; Check if a result is an error
(error/anomaly? result)
;; Create an anomaly from an HTTP response
(error/from-http-response 404 {:message "Not Found"} :storage)
;; Create a domain-specific anomaly
(error/anomaly :cognitect.anomalies/incorrect
{:supabase/service :auth
:cognitect.anomalies/message "Invalid credentials"})(anomaly category)(anomaly category extra)Creates an anomaly map with the given category and optional extra fields.
category must be a valid cognitect.anomalies category keyword.
(anomaly :cognitect.anomalies/incorrect
{:cognitect.anomalies/message "Bad request"
:supabase/service :auth})
Creates an anomaly map with the given `category` and optional extra fields.
`category` must be a valid `cognitect.anomalies` category keyword.
(anomaly :cognitect.anomalies/incorrect
{:cognitect.anomalies/message "Bad request"
:supabase/service :auth})(anomaly? x)Returns true if x is an anomaly map (contains :cognitect.anomalies/category).
Returns true if `x` is an anomaly map (contains `:cognitect.anomalies/category`).
(from-exception ex)(from-exception ex service)Creates an anomaly map from a caught exception.
(try (do-something) (catch Exception e (error/from-exception e :auth)))
Creates an anomaly map from a caught exception.
(try
(do-something)
(catch Exception e
(error/from-exception e :auth)))(from-http-response status body)(from-http-response status body service)Creates an anomaly map from an HTTP error response.
status is the HTTP status code (>= 400). body is the parsed response body.
service is an optional keyword identifying the Supabase service.
(from-http-response 404 {:message "Not found"} :storage)
;; => {:cognitect.anomalies/category :cognitect.anomalies/not-found
;; :cognitect.anomalies/message "Not Found"
;; :supabase/service :storage
;; :supabase/code :not-found
;; :http/status 404
;; :http/body {:message "Not found"}}
Creates an anomaly map from an HTTP error response.
`status` is the HTTP status code (>= 400). `body` is the parsed response body.
`service` is an optional keyword identifying the Supabase service.
(from-http-response 404 {:message "Not found"} :storage)
;; => {:cognitect.anomalies/category :cognitect.anomalies/not-found
;; :cognitect.anomalies/message "Not Found"
;; :supabase/service :storage
;; :supabase/code :not-found
;; :http/status 404
;; :http/body {:message "Not found"}}(humanize-code code)Converts a keyword error code to a human-readable string.
(humanize-code :not-found) ;; => "Not Found" (humanize-code :bad-request) ;; => "Bad Request"
Converts a keyword error code to a human-readable string. (humanize-code :not-found) ;; => "Not Found" (humanize-code :bad-request) ;; => "Bad Request"
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |