Skip to content

camertron/absolver

Repository files navigation

absolver

Absolver is a small little gem that allows setting the cause of a Ruby exception. Consider this example:

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:

err        # => #<RuntimeError: foo>
err.cause  # => #<ZeroDivisionError: divided by 0>

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:

err.cause = nil
# NoMethodError (undefined method `cause=' for #<RuntimeError: foo>)

What's a programmer to do? Use absolver!

require 'absolver'

Absolver.set_cause(err, nil)

err.cause  # => nil

Absolver uses Fiddle 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:

require 'absolver/exception'
err.cause = nil

License

Licensed under the MIT license. See LICENSE for details.

Authors

About

Allows setting the cause of Ruby exceptions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages