Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/camertron/absolver
Allows setting the cause of Ruby exceptions.
https://github.com/camertron/absolver
Last synced: 18 days ago
JSON representation
Allows setting the cause of Ruby exceptions.
- Host: GitHub
- URL: https://github.com/camertron/absolver
- Owner: camertron
- License: mit
- Created: 2019-02-27T17:45:52.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-27T18:00:45.000Z (almost 6 years ago)
- Last Synced: 2024-04-25T07:22:25.154Z (9 months ago)
- Language: Ruby
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
absolver
===Absolver is a small little gem that allows setting the cause of a Ruby exception. Consider this example:
```ruby
err = begin
begin
1 / 0
rescue => e
raise RuntimeError, 'foo'
end
rescue => e
e
end
```Notice that I raised a `RuntimeError` _inside_ a `rescue` block. In Ruby 2.1 and later, doing so will set the inner exception's `#cause` attribute to the outer exception. In other words:
```ruby
err # => #
err.cause # => #
```Cool, thanks Ruby!
What if I want to change the cause though? Maybe I don't really want to include the cause when reporting to an external error monitoring service, for example. Sadly, Ruby doesn't let you do that:
```ruby
err.cause = nil
# NoMethodError (undefined method `cause=' for #)
```What's a programmer to do? Use absolver!
```ruby
require 'absolver'Absolver.set_cause(err, nil)
err.cause # => nil
```Absolver uses [Fiddle](https://ruby-doc.org/stdlib-2.5.0/libdoc/fiddle/rdoc/Fiddle.html) to safely set the `cause` attribute of the `Exception` object. Basically it reaches down into Ruby's internals to adjust the value of the `cause` instance variable, which normally isn't exposed to your program.
Absolver also includes a small monkeypatch that adds `cause=` to `Exception`. Because many people dislike monkeypatches, you have to opt-in:
```ruby
require 'absolver/exception'
err.cause = nil
```## License
Licensed under the MIT license. See LICENSE for details.
## Authors
* Cameron C. Dutro: http://github.com/camertron