Skip to content

Commit 9c00fdb

Browse files
authored
fix: compatibility fixes for ActionMailer plugin (#1146)
1 parent cae70e4 commit 9c00fdb

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

lib/rollbar/plugins/active_job.rb

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@ module Rollbar
88
module ActiveJob
99
def self.included(base)
1010
base.send :rescue_from, Exception do |exception|
11-
args = if self.class.respond_to?(:log_arguments?) && !self.class.log_arguments?
12-
arguments.map(&Rollbar::Scrubbers.method(:scrub_value))
13-
else
14-
arguments
15-
end
16-
1711
job_data = {
1812
:job => self.class.name,
19-
:use_exception_level_filters => true,
20-
:arguments => args
13+
:use_exception_level_filters => true
2114
}
2215

23-
# job_id isn't present when this is a mailer class.
24-
job_data[:job_id] = job_id if defined?(job_id)
16+
# When included in ActionMailer, the handler is called twice.
17+
# This detects the execution that has the expected state.
18+
if defined?(ActionMailer::Base) && self.class.ancestors.include?(ActionMailer::Base)
19+
job_data[:action] = action_name
20+
job_data[:params] = @params
21+
22+
Rollbar.error(exception, job_data)
23+
24+
# This detects other supported integrations.
25+
elsif defined?(arguments)
26+
job_data[:arguments] = \
27+
if self.class.respond_to?(:log_arguments?) && !self.class.log_arguments?
28+
arguments.map(&Rollbar::Scrubbers.method(:scrub_value))
29+
else
30+
arguments
31+
end
32+
job_data[:job_id] = job_id if defined?(job_id)
33+
34+
Rollbar.error(exception, job_data)
35+
end
2536

26-
Rollbar.error(exception, job_data)
2737
raise exception
2838
end
2939
end

spec/rollbar/plugins/active_job_spec.rb

+35-26
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ def perform(exception, job_id)
2222
end
2323
end
2424

25-
class TestMailer < ActionMailer::Base
26-
attr_accessor :arguments
27-
28-
def test_email(*_arguments)
29-
error = StandardError.new('oh no')
30-
raise(error)
31-
end
32-
end
33-
3425
before { reconfigure_notifier }
3526

3627
let(:exception) { StandardError.new('oh no') }
@@ -52,19 +43,6 @@ def test_email(*_arguments)
5243
end.to raise_error exception
5344
end
5445

55-
it 'scrubs all arguments if job has `log_arguments` disabled' do
56-
allow(TestJob).to receive(:log_arguments?).and_return(false)
57-
58-
expected_params[:job_id] = job_id
59-
expected_params[:arguments] = ['******', '******', '******']
60-
expect(Rollbar).to receive(:error).with(exception, expected_params)
61-
begin
62-
TestJob.new(1, 2, 3).perform(exception, job_id)
63-
rescue StandardError
64-
nil
65-
end
66-
end
67-
6846
it 'job is created' do
6947
ActiveJob::Base.queue_adapter = :test
7048
expect do
@@ -88,6 +66,15 @@ def test_email(*_arguments)
8866
end
8967

9068
context 'using ActionMailer::DeliveryJob', :if => Gem::Version.new(Rails.version) < Gem::Version.new('6.0') do
69+
class TestMailer < ActionMailer::Base
70+
attr_accessor :arguments
71+
72+
def test_email(*_arguments)
73+
error = StandardError.new('oh no')
74+
raise(error)
75+
end
76+
end
77+
9178
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)
9279

9380
it_behaves_like 'an ActiveMailer plugin'
@@ -118,6 +105,19 @@ def test_email(*_arguments)
118105
end
119106
end
120107

108+
it 'scrubs all arguments if job has `log_arguments` disabled' do
109+
allow(TestJob).to receive(:log_arguments?).and_return(false)
110+
111+
expected_params[:job_id] = job_id
112+
expected_params[:arguments] = ['******', '******', '******']
113+
expect(Rollbar).to receive(:error).with(exception, expected_params)
114+
begin
115+
TestJob.new(1, 2, 3).perform(exception, job_id)
116+
rescue StandardError
117+
nil
118+
end
119+
end
120+
121121
it 'scrubs job arguments hash' do
122122
Rollbar.configure do |config|
123123
config.scrub_fields |= ['user_id']
@@ -155,12 +155,22 @@ def test_email(*_arguments)
155155
end
156156

157157
context 'using ActionMailer::MailDeliveryJob', :if => Gem::Version.new(Rails.version) >= Gem::Version.new('6.0') do
158+
class ParentMailer < ActionMailer::Base; end
159+
160+
class TestMailer2 < ParentMailer
161+
def test_email(*_arguments)
162+
error = StandardError.new('oh no')
163+
raise(error)
164+
end
165+
end
166+
158167
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)
159168

160169
let(:expected_params) do
161170
{
162171
:job => 'TestJob',
163-
:use_exception_level_filters => true
172+
:use_exception_level_filters => true,
173+
:action => 'test_email'
164174
}
165175
end
166176

@@ -169,13 +179,12 @@ def test_email(*_arguments)
169179
it 'reports the error to Rollbar' do
170180
expected_params.delete(:job)
171181

172-
# In 6+, the re-raise in the plugin will cause the rescue_from to be called twice.
173-
expect(Rollbar).to receive(:error).twice.with(kind_of(StandardError),
182+
expect(Rollbar).to receive(:error).with(kind_of(StandardError),
174183
hash_including(expected_params))
175184

176185
perform_enqueued_jobs do
177186
begin
178-
TestMailer.test_email(argument).deliver_later
187+
TestMailer2.test_email(argument).deliver_later
179188
rescue StandardError => e
180189
nil
181190
end

0 commit comments

Comments
 (0)