Skip to content

Commit

Permalink
Handle cases where job is nil in Que error notifier
Browse files Browse the repository at this point in the history
This should fix #545
  • Loading branch information
Toby Hsieh authored and tomlongridge committed Aug 23, 2019
1 parent 73ab896 commit afca717
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

## TBD

### Fixes

* Handle `nil` values for the `job` block parameter for the Que error notifier.
This occurs under some conditions such as database connection failures.
| [#545](https://github.com/bugsnag/bugsnag-ruby/issues/545)
| [#548](https://github.com/bugsnag/bugsnag-ruby/pull/548)

## 6.11.1 (22 Jan 2019)

### Fixes
Expand Down
24 changes: 13 additions & 11 deletions lib/bugsnag/integrations/que.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
if defined?(::Que)
handler = proc do |error, job|
begin
job = job.dup # Make sure the original job object is not mutated.
job &&= job.dup # Make sure the original job object is not mutated.

Bugsnag.notify(error, true) do |report|
job[:error_count] += 1
if job
job[:error_count] += 1

# If the job was scheduled using ActiveJob then unwrap the job details for clarity:
if job[:job_class] == "ActiveJob::QueueAdapters::QueAdapter::JobWrapper"
wrapped_job = job[:args].last
wrapped_job = wrapped_job.each_with_object({}) { |(k, v), result| result[k.to_sym] = v } # Symbolize keys
# If the job was scheduled using ActiveJob then unwrap the job details for clarity:
if job[:job_class] == "ActiveJob::QueueAdapters::QueAdapter::JobWrapper"
wrapped_job = job[:args].last
wrapped_job = wrapped_job.each_with_object({}) { |(k, v), result| result[k.to_sym] = v } # Symbolize keys

# Align key names with keys in `job`
wrapped_job[:queue] = wrapped_job.delete(:queue_name)
wrapped_job[:args] = wrapped_job.delete(:arguments)
# Align key names with keys in `job`
wrapped_job[:queue] = wrapped_job.delete(:queue_name)
wrapped_job[:args] = wrapped_job.delete(:arguments)

job.merge!(wrapper_job_class: job[:job_class], wrapper_job_id: job[:job_id]).merge!(wrapped_job)
job.merge!(wrapper_job_class: job[:job_class], wrapper_job_id: job[:job_id]).merge!(wrapped_job)
end
end

report.add_tab(:job, job)
Expand All @@ -43,4 +45,4 @@
Bugsnag.configuration.app_type ||= "que"
Que.error_handler = handler
end
end
end
22 changes: 21 additions & 1 deletion spec/integrations/que_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
unless defined?(::Que)
@mocked_que = true
class ::Que
class << self
attr_accessor :error_notifier
end
end
module Kernel
alias_method :old_require, :require
Expand All @@ -31,7 +34,6 @@ def require(path)
expect(report).to receive(:add_tab).with(:job, {
:error_count => 1,
:job_class => 'ActiveJob::QueueAdapters::QueAdapter::JobWrapper',
:args => [{"queue_name" => "foo", "arguments" => "bar"}],
:job_id => "ID",
:wrapper_job_class => 'ActiveJob::QueueAdapters::QueAdapter::JobWrapper',
:wrapper_job_id => "ID",
Expand Down Expand Up @@ -60,6 +62,24 @@ def require(path)
load './lib/bugsnag/integrations/que.rb'
end

context 'when the job is nil' do
it 'notifies Bugsnag' do
load './lib/bugsnag/integrations/que.rb'
error = RuntimeError.new('nil job')
report = Bugsnag::Report.new(error, Bugsnag::Configuration.new)
expect(Bugsnag).to receive(:notify).with(error, true).and_yield(report)

Que.error_notifier.call(error, nil)

expect(report.meta_data['custom'].fetch('job')).to eq(nil)
expect(report.severity).to eq('error')
expect(report.severity_reason).to eq({
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
:attributes => {:framework => 'Que'},
})
end
end

after do
Object.send(:remove_const, :Que) if @mocked_que
module Kernel
Expand Down

0 comments on commit afca717

Please sign in to comment.