Skip to content

Commit

Permalink
Merge pull request #136 from jondeandres/support-cause-118
Browse files Browse the repository at this point in the history
Support nested exceptions for Ruby 2.1. Fixes #118.
  • Loading branch information
brianr committed Sep 30, 2014
2 parents 7d5a444 + 2931996 commit 372543c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 17 deletions.
47 changes: 36 additions & 11 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,37 @@ def exception_data(exception, force_level = nil)

data[:level] = force_level if force_level

traces = trace_chain(exception)

if traces.size > 1
body = {
:trace_chain => traces
}
elsif traces.size == 1
body = {
:trace => traces[0]
}
end

data[:body] = body

data[:server] = server_data

data
end

def trace_chain(exception)
traces = [trace_data(exception)]

while exception.respond_to?(:cause) && (cause = exception.cause)
traces << trace_data(cause)
exception = cause
end

traces
end

def trace_data(exception)
# parse backtrace
if exception.backtrace.respond_to?( :map )
frames = exception.backtrace.map { |frame|
Expand All @@ -324,19 +355,13 @@ def exception_data(exception, force_level = nil)
frames = []
end

data[:body] = {
:trace => {
:frames => frames,
:exception => {
:class => exception.class.name,
:message => exception.message
}
{
:frames => frames,
:exception => {
:class => exception.class.name,
:message => exception.message
}
}

data[:server] = server_data

data
end

def logger
Expand Down
12 changes: 7 additions & 5 deletions spec/requests/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
expect{ get 'current_user', nil, :cookie => '8%B' }.to raise_exception

Rollbar.last_report.should_not be_nil

exception_info = Rollbar.last_report[:body][:trace][:exception]
exception_info[:class].should == 'ArgumentError'
exception_info[:message].should == 'invalid %-encoding (8%B)'
Expand All @@ -44,11 +44,13 @@
end

it "should report uncaught exceptions" do
expect{ get 'current_user' }.to raise_exception
expect { get 'current_user' }.to raise_exception

body = Rollbar.last_report[:body]
trace = body[:trace] && body[:trace] || body[:trace_chain][0]

exception_info = Rollbar.last_report[:body][:trace][:exception]
exception_info[:class].should == 'NoMethodError'
# exception_info[:message].should == 'undefined method `-\' for "1":String'
trace[:exception][:class].should == 'NoMethodError'
trace[:exception][:message].should == 'undefined method `-\' for "1":String'
end
end
end
47 changes: 46 additions & 1 deletion spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def backtrace
end
end

context 'exception_data' do
describe '.exception_data' do
before(:each) do
configure
begin
Expand Down Expand Up @@ -647,6 +647,51 @@ def backtrace
end
end

context 'with nested exceptions' do
let(:crashing_code) do
proc do
begin
begin
fail CauseException.new('the cause')
rescue
fail StandardError.new('the error')
end
rescue => e
e
end
end
end
let(:rescued_exception) { crashing_code.call }

if Exception.instance_methods.include?(:cause)
it 'sends the two exceptions in the trace_chain attribute' do
data = Rollbar.send(:exception_data, rescued_exception)
body = data[:body]

body[:trace].should be_nil
body[:trace_chain].should be_kind_of(Array)

chain = body[:trace_chain]
chain[0][:exception][:class].should match(/StandardError/)
chain[0][:exception][:message].should match(/the error/)

chain[1][:exception][:class].should match(/CauseException/)
chain[1][:exception][:message].should match(/the cause/)
end

else
it 'sends only the last exception in the trace attribute' do
data = Rollbar.send(:exception_data, rescued_exception)
body = data[:body]

body[:trace].should be_kind_of(Hash)
body[:trace_chain].should be_nil

body[:trace][:exception][:class].should match(/StandardError/)
body[:trace][:exception][:message].should match(/the error/)
end
end
end
end

context 'logger' do
Expand Down
1 change: 1 addition & 0 deletions spec/support/cause_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class CauseException < StandardError; end

0 comments on commit 372543c

Please sign in to comment.