Skip to content

Instantly share code, notes, and snippets.

@3limin4t0r
Created September 13, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3limin4t0r/79ad4cc9b3886dea60d112aa2f7db40e to your computer and use it in GitHub Desktop.
Save 3limin4t0r/79ad4cc9b3886dea60d112aa2f7db40e to your computer and use it in GitHub Desktop.
class ExceptionNegator < BasicObject
def initialize(object, *exceptions)
@object = object
exceptions << ::StandardError if exceptions.empty?
@exceptions = exceptions
end
def __error__
@error
end
def method_missing(name, *args, &block)
return super unless @object.respond_to?(name)
@error = nil
begin
@object.public_send(name, *args, &block)
rescue *@exceptions => error
@error = error
nil
end
end
def respond_to_missing?(name, include_private = false)
@object.respond_to?(name, include_private) or super
end
end
my_magic_jira = ExceptionNegator.new(japi.new(...), JiraError, ArgumentError)
# wrapped object ^ ^ ^ exceptions to negate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment