Created
January 3, 2014 23:19
-
-
Save pda/8248619 to your computer and use it in GitHub Desktop.
Recursively inspect Exception#cause to trace where the error condition originated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #<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