Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lpil/snag
⛵ A boilerplate-free ad-hoc error type
https://github.com/lpil/snag
error-handling gleam
Last synced: 21 days ago
JSON representation
⛵ A boilerplate-free ad-hoc error type
- Host: GitHub
- URL: https://github.com/lpil/snag
- Owner: lpil
- License: apache-2.0
- Created: 2021-03-22T12:02:58.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-19T13:43:42.000Z (23 days ago)
- Last Synced: 2024-11-19T14:35:04.678Z (23 days ago)
- Topics: error-handling, gleam
- Language: Gleam
- Homepage:
- Size: 28.3 KB
- Stars: 54
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-gleam - snag - [📚](https://hexdocs.pm/snag/) - A boilerplate-free ad-hoc error type (Packages / Error Handling)
README
# Snag
A Snag is a boilerplate-free ad-hoc error type.
Use `Result(value, Snag)` (or the `snag.Result(value)` alias) in functions
that may fail.A low level message like "Unexpected status 401" or "No such file or
directory" can be confusing or difficult to debug, so use the `snag.context`
function to add extra contextual information.```gleam
import gleam/io
import gleam/result
import my_app.{User}
import snag.{type Result}pub fn log_in(user_id: Int) -> Result(User) {
use api_key <- result.try(
my_app.read_file("api_key.txt")
|> snag.context("Could not load API key")
)use session_token <- result.try(
user_id
|> my_app.create_session(api_key)
|> snag.context("Session creation failed")
)Ok(session_token)
}pub fn main() {
case log_in(42) {
Ok(session) -> io.println("Logged in!")
Error(snag) -> {
io.print(snag.pretty_print(snag))
my_app.exit(1)
}
}
}
```In this code when an error occurs within the `create_session` function an
error message like this is printed using the added contextual information:```text
error: Session creation failedcause:
0: Unable to exchange token with authentication service
1: Service authentication failed
2: Unexpected HTTP status 401
```## When should I use Snag?
Snag is useful in code where it must either pass or fail, and when it fails we
want good debugging information to print to the user. i.e. Command line
tools, data processing pipelines, etc. Here Snag provides a convenient way to
create errors with a reasonable amount of debugging information, without the
boilerplate of a custom error type.It is not suited to code where the application needs to make a decision about
what to do in the event of an error, such as whether to give up or to try
again. i.e. Libraries, web application backends, API clients, etc. In these
situations it is recommended to create a custom type for your errors as it
can be pattern matched on and have any additional detail added as fields.## Installation
Add `snag` to your Gleam project
```
gleam add snag
```## Prior art
This library is inspired by the following projects:
- Rust's [`anyhow`](https://github.com/dtolnay/anyhow) and
[`std::error::Error`](https://doc.rust-lang.org/std/error/trait.Error.html)
- Go's [`error`](https://golang.org/pkg/errors/).