Skip to content

Commit

Permalink
fix: compatibility fixes for ActionMailer plugin (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Jan 11, 2024
1 parent cae70e4 commit 9c00fdb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
32 changes: 21 additions & 11 deletions lib/rollbar/plugins/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ module Rollbar
module ActiveJob
def self.included(base)
base.send :rescue_from, Exception do |exception|
args = if self.class.respond_to?(:log_arguments?) && !self.class.log_arguments?
arguments.map(&Rollbar::Scrubbers.method(:scrub_value))
else
arguments
end

job_data = {
:job => self.class.name,
:use_exception_level_filters => true,
:arguments => args
:use_exception_level_filters => true
}

# job_id isn't present when this is a mailer class.
job_data[:job_id] = job_id if defined?(job_id)
# When included in ActionMailer, the handler is called twice.
# This detects the execution that has the expected state.
if defined?(ActionMailer::Base) && self.class.ancestors.include?(ActionMailer::Base)
job_data[:action] = action_name
job_data[:params] = @params

Rollbar.error(exception, job_data)

# This detects other supported integrations.
elsif defined?(arguments)
job_data[:arguments] = \
if self.class.respond_to?(:log_arguments?) && !self.class.log_arguments?
arguments.map(&Rollbar::Scrubbers.method(:scrub_value))
else
arguments
end
job_data[:job_id] = job_id if defined?(job_id)

Rollbar.error(exception, job_data)
end

Rollbar.error(exception, job_data)
raise exception
end
end
Expand Down
61 changes: 35 additions & 26 deletions spec/rollbar/plugins/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ def perform(exception, job_id)
end
end

class TestMailer < ActionMailer::Base
attr_accessor :arguments

def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
end
end

before { reconfigure_notifier }

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

it 'scrubs all arguments if job has `log_arguments` disabled' do
allow(TestJob).to receive(:log_arguments?).and_return(false)

expected_params[:job_id] = job_id
expected_params[:arguments] = ['******', '******', '******']
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(1, 2, 3).perform(exception, job_id)
rescue StandardError
nil
end
end

it 'job is created' do
ActiveJob::Base.queue_adapter = :test
expect do
Expand All @@ -88,6 +66,15 @@ def test_email(*_arguments)
end

context 'using ActionMailer::DeliveryJob', :if => Gem::Version.new(Rails.version) < Gem::Version.new('6.0') do
class TestMailer < ActionMailer::Base
attr_accessor :arguments

def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
end
end

include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)

it_behaves_like 'an ActiveMailer plugin'
Expand Down Expand Up @@ -118,6 +105,19 @@ def test_email(*_arguments)
end
end

it 'scrubs all arguments if job has `log_arguments` disabled' do
allow(TestJob).to receive(:log_arguments?).and_return(false)

expected_params[:job_id] = job_id
expected_params[:arguments] = ['******', '******', '******']
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(1, 2, 3).perform(exception, job_id)
rescue StandardError
nil
end
end

it 'scrubs job arguments hash' do
Rollbar.configure do |config|
config.scrub_fields |= ['user_id']
Expand Down Expand Up @@ -155,12 +155,22 @@ def test_email(*_arguments)
end

context 'using ActionMailer::MailDeliveryJob', :if => Gem::Version.new(Rails.version) >= Gem::Version.new('6.0') do
class ParentMailer < ActionMailer::Base; end

class TestMailer2 < ParentMailer
def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
end
end

include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper)

let(:expected_params) do
{
:job => 'TestJob',
:use_exception_level_filters => true
:use_exception_level_filters => true,
:action => 'test_email'
}
end

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

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

perform_enqueued_jobs do
begin
TestMailer.test_email(argument).deliver_later
TestMailer2.test_email(argument).deliver_later
rescue StandardError => e
nil
end
Expand Down

0 comments on commit 9c00fdb

Please sign in to comment.