https://github.com/coast-framework/error
Easy clojure error handling
https://github.com/coast-framework/error
clojure error-handling errors
Last synced: 6 months ago
JSON representation
Easy clojure error handling
- Host: GitHub
- URL: https://github.com/coast-framework/error
- Owner: coast-framework
- License: mit
- Created: 2019-06-20T20:37:33.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-18T18:04:12.000Z (over 6 years ago)
- Last Synced: 2025-06-11T14:44:11.330Z (7 months ago)
- Topics: clojure, error-handling, errors
- Language: Clojure
- Size: 19.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# error
Easy clojure error handling
## Installation
Make your `deps.edn` look like this:
```clojure
; ... paths probably
{:deps {coast-framework/error {:mvn/version "1.0.2"}}}
;... your other libraries}}
; ... rest of deps.edn
```
## Usage
Require it like this
```clojure
(ns your-project
(:require [error.core :as error]))
```
Use it like this
```clojure
(defn validate-email [email]
(if (re-matches #".+@.+\..+" email)
email
(error/raise (str "Please enter a valid email address, not " email))))
(let [[email e] (error/rescue (validate-email "not an email"))]
(if (some? e)
{:error e}
{:email email}))
```
or like this
```clojure
(defn validate-email [email]
(if (re-matches #".+@.+\..+" email)
email
(error/raise (str "Please enter a valid email address, not " email))))
:error/email
(let [[email e] (error/rescue (validate-email "not an email")
:error/email)]
(if (some? e)
{:error/email e}
{:email email}))
```
It will still throw on exceptions if the identifier you specify isn't the same
```clojure
(error/rescue
(error/raise "error" :error)
:error1)
; :error1 != :error so the following exception will be called
(throw (ex-info "error" {:error.core/e "error" :error.core/id :error}))
```
It also works with regular exceptions with `try*`
```clojure
(defn validate-email [email]
(if (re-matches #".+@.+\..+" email)
email
(throw (Exception. (str "Please enter a valid email address, not " email)))))
(let [[email e] (error/try* (validate-email "not an email"))]
(if (some? e)
{:error/email (.getMessage e)}
{:email email}))
```
## Testing
```sh
git clone ...
cd error
make test
```
## License
MIT
## Contribution
Any and all issues or pull requests welcome!
## Too Small
This library is really small, it's true. Too small? Could be.