Skip to content

Commit

Permalink
Merge pull request #290 from rollbar/send-last-backrace-entry-on-fail…
Browse files Browse the repository at this point in the history
…safe

Send nearest backtrace entry on failsafe messages. @brianr.
  • Loading branch information
jondeandres committed Sep 15, 2015
2 parents 4ef7656 + d0f505b commit 7a0f5af
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
25 changes: 13 additions & 12 deletions gemfiles/rails30.gemfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# This file was generated by Appraisal

source "https://rubygems.org"
source 'https://rubygems.org'

gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
gem "jruby-openssl", :platform => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
gem "appraisal", '= 1.0.2'
gem "rubysl", "~> 2.0", :platform => :rbx
gem "racc", :platform => :rbx
gem "minitest", :platform => :rbx
gem "rubysl-test-unit", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "3.0.20"
gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
gem 'jruby-openssl', :platform => :jruby
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
gem 'appraisal', '= 1.0.2'
gem 'rubysl', '~> 2.0', :platform => :rbx
gem 'racc', :platform => :rbx
gem 'minitest', :platform => :rbx
gem 'rubysl-test-unit', :platform => :rbx
gem 'rubinius-developer_tools', :platform => :rbx
gem 'rails', '3.0.20'
gem 'hitimes', '< 1.2.2'

gemspec :path => "../"
gemspec :path => '../'
46 changes: 32 additions & 14 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,11 @@ 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}."
exception_reason = failsafe_reason(message, exception)

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
log_error "[Rollbar] Sending failsafe response due to #{exception_reason}"

body = failsafe_body(exception_reason)

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

def failsafe_reason(message, exception)
body = ''

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}"
rescue
end
else
begin
body += message.to_s
rescue
end
end

body
end

def failsafe_body(reason)
"Failsafe from rollbar-gem. #{reason}"
end

def schedule_payload(payload)
return if payload.nil?

Expand Down
27 changes: 23 additions & 4 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1564,25 +1564,44 @@ 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'
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

context 'without exception object' do
it 'just sends the given message' do
sent_payload = notifier.send(:send_failsafe, "test failsafe", nil)

expected_message = 'Failsafe from rollbar-gem: test failsafe'
expected_message = 'Failsafe from rollbar-gem. test failsafe'
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'] }
let(:failsafe_reason) { 'StandardError in func3: test failsafe' }
let(:expected_body) { "Failsafe from rollbar-gem. #{failsafe_reason}" }
let(:expected_log_message) do
"[Rollbar] Sending failsafe response due to #{failsafe_reason}"
end

before { exception.set_backtrace(backtrace) }

it 'adds the nearest frame to the message' do
expect(notifier).to receive(:log_error).with(expected_log_message)

sent_payload = notifier.send(:send_failsafe, "test failsafe", exception)

expect(sent_payload['data'][:body][:message][:body]).to be_eql(expected_body)
end
end
end

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

0 comments on commit 7a0f5af

Please sign in to comment.