error
Version, currently 0.2.02 versions
- 0.2.0latestJun 10, 2019
- 0.1.0not indexedJun 10, 2019
github.com/j8r/error.cr
Efficient errors without raising - no expensive stack unwinding
8 stars
0 dependents
License: ISC
Installation
# Add this to your shard.yml
dependencies:
error:
github: j8r/error.cr
version: ~> 0.2.0Then run:
shards installshard.yml
- Crystal
- no constraint declared
- License
- ISC
- Author
- Julien Reichardt <mi@jrei.ch>
Dependencies
This version declares no dependencies.
README
# Crystal Error
[](https://cloud.drone.io/j8r/error.cr)
[](https://en.wikipedia.org/wiki/ISC_license)
An efficient way to handle errors without using `raise` - no expensive stack unwinding.
## Advantages
Over "classic" `Exception` errors, this approach provides:
- Shorter traces without stdlib related calls
- More useful output with the object and method called
- More performant than unwinding the stack (what `raise` does)
The main disadvantage is the slight verbosity added, having a new type `Error` type to handle.
On the other side `begin/rescue` aren't needed
## Installation
Add the dependency to your `shard.yml`:
```yaml
dependencies:
error:
github: j8r/error.cr
```
## Usage
### Simple error
```crystal
require "error"
def it_throws
Error.throw "Oops!"
end
p it_throws
```
result:
```
Oops! (Error)
from myapp.cr:4 in 'it_throws'
```
### Handling errors
```crystal
require "error"
struct Obj
def action : String | Error
Error.throw("Shouldn't be true") || "normal"
end
end
def main_program : String | Error
case result = Obj.new.action
# Throw the error on the top.
# Optionaly, the message can be modified and a parent error specified.
when Error then Error.throw "Action not successful: #{result}", result
else "operation successful: " + result
end
end
p main_program
```
result:
```
Action not successful: Shouldn't be true (Error)
from myapp.cr:5 in 'Obj#action'
from myapp.cr:13 in 'main_program'
```
### Custom errors
```crystal
class CustomError < Error
@message = "This is a custom error message"
end
def action
CustomError.throw
end
p action
```
result:
```
This is a custom error message (CustomError)
from app.cr:6 in 'action'
```
## FAQ
### I can't use this in my main program.
This libary can only be used inside methods, because the `Error.throw` macro expands to a `return Error`.
### This doesn't work in the `initialize` of my class!
A class initializer can't return an union, only an instance of the class.
You can create a `self.new` method that returns the union of the class and `Error`.
## License
Copyright (c) 2018-2019 Julien Reichardt - ISC License
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.2.0- Tagged
- Jun 10, 2019
- Commit
83b264703b00- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/j8r/error.cr
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 14, 2026
- Synced
- Aug 14, 2026
- Versions
- 2