Skip to content

Commit

Permalink
Send nearest backtrace entry on failsafe messages. @brianr.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon de Andres committed Sep 4, 2015
1 parent a8e5b0a commit 9980bee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
43 changes: 28 additions & 15 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,7 @@ def do_write_payload(payload)
end

def send_failsafe(message, exception)
body = 'Failsafe from rollbar-gem: '
log_error "[Rollbar] Sending failsafe response due to #{message}."

if exception
begin
body += "#{exception.class.name}: #{message}"
log_error "[Rollbar] #{exception.class.name}: #{exception}"
rescue
end
else
begin
body += message.to_s
rescue
end
end
body = failsafe_body(message, exception)

failsafe_data = {
:level => 'error',
Expand Down Expand Up @@ -619,6 +605,33 @@ def send_failsafe(message, exception)
failsafe_payload
end

def failsafe_body(message, exception)
body = 'Failsafe from rollbar-gem. '
log_error "[Rollbar] Sending failsafe response due to #{message}."

if exception
begin
backtrace = exception.backtrace || []
nearest_frame = backtrace[0]

exception_info = exception.class.name
exception_info += " in #{nearest_frame}" if nearest_frame

body += "#{exception_info}: #{message}"

log_error "[Rollbar] #{body}"
rescue
end
else
begin
body += message.to_s
rescue
end
end

body
end

def schedule_payload(payload)
return if payload.nil?

Expand Down
17 changes: 15 additions & 2 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1564,14 +1564,14 @@ def backtrace
context "send_failsafe" do
let(:exception) { StandardError.new }

it "should not crash when given a message and exception" do
it "doesn't crash when given a message and exception" do
sent_payload = notifier.send(:send_failsafe, "test failsafe", exception)

expected_message = 'Failsafe from rollbar-gem: StandardError: test failsafe'
expect(sent_payload['data'][:body][:message][:body]).to be_eql(expected_message)
end

it "should not crash when given all nils" do
it "doesn't crash when given all nils" do
notifier.send(:send_failsafe, nil, nil)
end

Expand All @@ -1583,6 +1583,19 @@ def backtrace
expect(sent_payload['data'][:body][:message][:body]).to be_eql(expected_message)
end
end

context 'if the exception has a backtrace' do
let(:backtrace) { ['func3', 'func2', 'func1'] }

before { exception.set_backtrace(backtrace) }

it 'adds the nearest frame to the message' do
sent_payload = notifier.send(:send_failsafe, "test failsafe", exception)

expected_message = 'Failsafe from rollbar-gem: StandardError in func3: test failsafe'
expect(sent_payload['data'][:body][:message][:body]).to be_eql(expected_message)
end
end
end

context 'when reporting internal error with nil context' do
Expand Down

0 comments on commit 9980bee

Please sign in to comment.