Cognitect Anomalies - standardized error categories.
Based on https://github.com/cognitect-labs/anomalies
Anomalies are maps with ::category key indicating the error type. These map to HTTP status codes:
| Category | HTTP | Description |
|---|---|---|
| ::unavailable | 503 | Service temporarily unavailable, retry later |
| ::interrupted | 500 | Operation was interrupted |
| ::incorrect | 400 | Bad request, invalid input |
| ::forbidden | 403 | Not authorized to perform this action |
| ::unauthorized | 401 | Authentication required |
| ::not-found | 404 | Resource not found |
| ::conflict | 409 | Conflict with current state (e.g., duplicate) |
| ::fault | 500 | Internal server error |
| ::busy | 503 | Server is busy, retry later |
| ::unsupported | 501 | Operation not supported |
Usage: (throw! ::forbidden "You are not authorized to access this resource") (throw! ::not-found "Therapy plan not found" {:plan-id plan-id}) (throw! ::incorrect "Invalid email format" {:field :email})
Cognitect Anomalies - standardized error categories.
Based on https://github.com/cognitect-labs/anomalies
Anomalies are maps with ::category key indicating the error type.
These map to HTTP status codes:
| Category | HTTP | Description |
|---------------|------|------------------------------------------------|
| ::unavailable | 503 | Service temporarily unavailable, retry later |
| ::interrupted | 500 | Operation was interrupted |
| ::incorrect | 400 | Bad request, invalid input |
| ::forbidden | 403 | Not authorized to perform this action |
| ::unauthorized| 401 | Authentication required |
| ::not-found | 404 | Resource not found |
| ::conflict | 409 | Conflict with current state (e.g., duplicate) |
| ::fault | 500 | Internal server error |
| ::busy | 503 | Server is busy, retry later |
| ::unsupported | 501 | Operation not supported |
Usage:
(throw! ::forbidden "You are not authorized to access this resource")
(throw! ::not-found "Therapy plan not found" {:plan-id plan-id})
(throw! ::incorrect "Invalid email format" {:field :email})(anomaly category message)(anomaly category message data)Creates an anomaly map.
Params:
category - Keyword. One of the anomaly categories.
message - String. Human-readable error message.
data - Map, optional. Additional context data.
Returns: Map. Anomaly with ::category, ::message, and any additional data.
Examples: (anomaly ::not-found "User not found") => {::category ::not-found, ::message "User not found"}
(anomaly ::forbidden "Access denied" {:user-id 123}) => {::category ::forbidden, ::message "Access denied", :user-id 123}
Creates an anomaly map.
Params:
`category` - Keyword. One of the anomaly categories.
`message` - String. Human-readable error message.
`data` - Map, optional. Additional context data.
Returns:
Map. Anomaly with ::category, ::message, and any additional data.
Examples:
(anomaly ::not-found "User not found")
=> {::category ::not-found, ::message "User not found"}
(anomaly ::forbidden "Access denied" {:user-id 123})
=> {::category ::forbidden, ::message "Access denied", :user-id 123}(anomaly? x)Returns true if x is an anomaly map.
Params:
x - Any. Value to check.
Returns: Boolean.
Returns true if x is an anomaly map. Params: `x` - Any. Value to check. Returns: Boolean.
Set of all valid anomaly categories.
Set of all valid anomaly categories.
Maps anomaly category to HTTP status code.
Maps anomaly category to HTTP status code.
(client-error? anomaly)Returns true if anomaly represents a client error (4xx).
Params:
anomaly - Map. Anomaly to check.
Returns: Boolean.
Returns true if anomaly represents a client error (4xx). Params: `anomaly` - Map. Anomaly to check. Returns: Boolean.
(conflict! message)(conflict! message data)Throws conflict (409) anomaly.
Params:
message - String. Error message describing the conflict.
data - Map, optional. Additional context.
Throws conflict (409) anomaly. Params: `message` - String. Error message describing the conflict. `data` - Map, optional. Additional context.
(ex->anomaly e)Converts any exception to an anomaly.
If the exception already contains anomaly data, returns that. Otherwise, wraps it as a ::fault anomaly.
Params:
e - Exception. The exception to convert.
Returns: Map. An anomaly map.
Converts any exception to an anomaly. If the exception already contains anomaly data, returns that. Otherwise, wraps it as a ::fault anomaly. Params: `e` - Exception. The exception to convert. Returns: Map. An anomaly map.
(ex-anomaly e)Extracts anomaly from an exception, if present.
Params:
e - Exception. The exception to extract from.
Returns: Map or nil. The anomaly map if exception contains one, nil otherwise.
Extracts anomaly from an exception, if present. Params: `e` - Exception. The exception to extract from. Returns: Map or nil. The anomaly map if exception contains one, nil otherwise.
(fault! message)(fault! message data)Throws fault (500) anomaly for internal errors.
Params:
message - String. Error message (keep it generic for security).
data - Map, optional. Additional context (for logging, not exposed to client).
Throws fault (500) anomaly for internal errors. Params: `message` - String. Error message (keep it generic for security). `data` - Map, optional. Additional context (for logging, not exposed to client).
(forbidden! message)(forbidden! message data)Throws forbidden (403) anomaly.
Params:
message - String. Error message describing why access is denied.
data - Map, optional. Additional context.
Throws forbidden (403) anomaly. Params: `message` - String. Error message describing why access is denied. `data` - Map, optional. Additional context.
(http-status anomaly)Returns HTTP status code for an anomaly.
Params:
anomaly - Map. Anomaly with ::category key.
Returns: Integer. HTTP status code (defaults to 500 for unknown categories).
Returns HTTP status code for an anomaly. Params: `anomaly` - Map. Anomaly with ::category key. Returns: Integer. HTTP status code (defaults to 500 for unknown categories).
(incorrect! message)(incorrect! message data)Throws incorrect (400) anomaly for validation errors.
Params:
message - String. Error message describing what's invalid.
data - Map, optional. Additional context (e.g., field names, validation errors).
Throws incorrect (400) anomaly for validation errors. Params: `message` - String. Error message describing what's invalid. `data` - Map, optional. Additional context (e.g., field names, validation errors).
(not-found! message)(not-found! message data)Throws not-found (404) anomaly.
Params:
message - String. Error message describing what wasn't found.
data - Map, optional. Additional context.
Throws not-found (404) anomaly. Params: `message` - String. Error message describing what wasn't found. `data` - Map, optional. Additional context.
(server-error? anomaly)Returns true if anomaly represents a server error (5xx).
Params:
anomaly - Map. Anomaly to check.
Returns: Boolean.
Returns true if anomaly represents a server error (5xx). Params: `anomaly` - Map. Anomaly to check. Returns: Boolean.
(throw! category message)(throw! category message data)Throws an ex-info exception with anomaly data.
Params:
category - Keyword. One of the anomaly categories.
message - String. Human-readable error message.
data - Map, optional. Additional context data.
Throws: ExceptionInfo with anomaly map as ex-data.
Examples: (throw! ::forbidden "You cannot access this resource") (throw! ::not-found "Patient not found" {:patient-id pid})
Throws an ex-info exception with anomaly data.
Params:
`category` - Keyword. One of the anomaly categories.
`message` - String. Human-readable error message.
`data` - Map, optional. Additional context data.
Throws:
ExceptionInfo with anomaly map as ex-data.
Examples:
(throw! ::forbidden "You cannot access this resource")
(throw! ::not-found "Patient not found" {:patient-id pid})(unauthorized!)(unauthorized! message)(unauthorized! message data)Throws unauthorized (401) anomaly.
Params:
message - String, optional. Error message. Defaults to "Authentication required".
data - Map, optional. Additional context.
Throws unauthorized (401) anomaly. Params: `message` - String, optional. Error message. Defaults to "Authentication required". `data` - Map, optional. Additional context.
(unavailable! message)(unavailable! message data)Throws unavailable (503) anomaly.
Params:
message - String. Error message describing why service is unavailable.
data - Map, optional. Additional context.
Throws unavailable (503) anomaly. Params: `message` - String. Error message describing why service is unavailable. `data` - Map, optional. Additional context.
(unsupported! message)(unsupported! message data)Throws unsupported (501) anomaly.
Params:
message - String. Error message describing what's not supported.
data - Map, optional. Additional context.
Throws unsupported (501) anomaly. Params: `message` - String. Error message describing what's not supported. `data` - Map, optional. Additional context.
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 |