Skip to content

Instantly share code, notes, and snippets.

@pda
Created January 3, 2014 23:19
Show Gist options
  • Select an option

  • Save pda/8248619 to your computer and use it in GitHub Desktop.

Select an option

Save pda/8248619 to your computer and use it in GitHub Desktop.
Recursively inspect Exception#cause to trace where the error condition originated.
name = ->(i){ "method_#{i}" }
methods = 10.times.map do |i|
define_method(name[i]) do
if i == 0
raise "Top level error from #{name[i]}"
else
begin
send name[i - 1]
rescue
raise "Error from rescue in #{name[i]}"
end
end
end
end
def explain(error, depth: 0)
print(" " * depth)
p error
explain(error.cause, depth: depth + 1) if error.cause
end
begin
send(methods[-1])
rescue => e
explain(e)
end
#<RuntimeError: Error from rescue in method_9>
#<RuntimeError: Error from rescue in method_8>
#<RuntimeError: Error from rescue in method_7>
#<RuntimeError: Error from rescue in method_6>
#<RuntimeError: Error from rescue in method_5>
#<RuntimeError: Error from rescue in method_4>
#<RuntimeError: Error from rescue in method_3>
#<RuntimeError: Error from rescue in method_2>
#<RuntimeError: Error from rescue in method_1>
#<RuntimeError: Top level error from method_0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment