-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Telebugs | ||
# WrappedError unwraps an error and its causes up to a certain depth. | ||
class WrappedError | ||
MAX_NESTED_ERRORS = 3 | ||
|
||
def initialize(error) | ||
@error = error | ||
end | ||
|
||
def unwrap | ||
error_list = [] | ||
error = @error | ||
|
||
while error && error_list.size < MAX_NESTED_ERRORS | ||
error_list << error | ||
error = error.cause | ||
end | ||
|
||
error_list | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestWrappedError < Minitest::Test | ||
def test_unwraps_errors_without_a_cause | ||
error = StandardError.new | ||
wrapped = Telebugs::WrappedError.new(error) | ||
|
||
assert_equal [error], wrapped.unwrap | ||
end | ||
|
||
def test_unwraps_no_more_than_3_nested_errors | ||
begin | ||
raise RuntimeError, "error 1" | ||
rescue => e1 | ||
begin | ||
raise RuntimeError, "error 2" | ||
rescue => e2 | ||
begin | ||
raise RuntimeError, "error 3" | ||
rescue => e3 | ||
begin | ||
raise RuntimeError, "error 4" | ||
rescue => e4 | ||
begin | ||
raise RuntimeError, "error 5" | ||
rescue => e5 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
wrapped = Telebugs::WrappedError.new(e5) | ||
unwrapped = wrapped.unwrap | ||
|
||
assert_equal 3, unwrapped.size | ||
assert_equal [e5, e4, e3], unwrapped | ||
end | ||
end |