Skip to content

Commit

Permalink
Merge pull request #107 from MindscapeHQ/formigarafa-patch-1
Browse files Browse the repository at this point in the history
include innerError with nested errors data
  • Loading branch information
UberMouse authored Mar 14, 2017
2 parents 9610308 + 82f069e commit 61c7a75
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Features:
- Raygun API url is now configurable via `Configuration.api_url`
- Added support for `Exception#cause` to be tracked as `innerError` on Raygun. Only supported on Ruby >= 2.1

## 1.3.0 (10/03/2017)

Expand Down
8 changes: 6 additions & 2 deletions lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ def client_details
end

def error_details(exception)
{
details = {
className: exception.class.to_s,
message: exception.message.to_s.encode('UTF-16', :undef => :replace, :invalid => :replace).encode('UTF-8'),
stackTrace: (exception.backtrace || []).map { |line| stack_trace_for(line) }
stackTrace: (exception.backtrace || []).map { |line| stack_trace_for(line) },
}

details.update(innerError: error_details(exception.cause)) if exception.respond_to?(:cause) && exception.cause

details
end

def stack_trace_for(line)
Expand Down
47 changes: 46 additions & 1 deletion test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ def test_error_details_with_nil_message
assert_equal expected_message, @client.send(:error_details, e)[:message]
end

def test_inner_error_details
oe = TestException.new("A test message")
oe.set_backtrace(["/some/folder/some_file.rb:123:in `some_method_name'"])

ie = TestException.new("Inner test message")
ie.set_backtrace(["/another/path/foo.rb:1234:in `block (3 levels) run'"])

e = nest_exceptions(oe, ie)

expected_hash = {
className: "ClientTest::TestException",
message: oe.message,
stackTrace: [
{ lineNumber: "123", fileName: "/some/folder/some_file.rb", methodName: "some_method_name" }
]
}

# test inner error according with its availability (ruby >= 2.1)
if e.respond_to? :cause
expected_hash[:innerError] = {
className: "ClientTest::TestException",
message: ie.message,
stackTrace: [
{ lineNumber: "1234", fileName: "/another/path/foo.rb", methodName: "block (3 levels) run"}
]
}
end

assert_equal expected_hash, @client.send(:error_details, e)
end

def test_client_details
expected_hash = {
name: Raygun::CLIENT_NAME,
Expand Down Expand Up @@ -589,6 +620,21 @@ def test_exception
e
end

def nest_exceptions(outer_exception, inner_exception)
begin
begin
raise inner_exception.class, inner_exception.message
rescue => e
e.set_backtrace inner_exception.backtrace
raise outer_exception.class, outer_exception.message
end
rescue => nested_exception
nested_exception.set_backtrace outer_exception.backtrace
end

nested_exception
end

def exception_hash
{
className: "ClientTest::TestException",
Expand Down Expand Up @@ -618,5 +664,4 @@ def sample_env_hash
"REMOTE_ADDR"=>"127.0.0.1"
}
end

end

0 comments on commit 61c7a75

Please sign in to comment.