diff options
author | Alberto Vena <kennyadsl@gmail.com> | 2021-07-20 16:30:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-20 16:30:43 +0200 |
commit | c6c9fea5898530405dff44f5603e9191a679d1a6 (patch) | |
tree | e7e511496ad1bb7b0b0d32607cf634332192d69b | |
parent | 662719693c9f369cd9176e432ab3d4a554822751 (diff) | |
parent | 0317e228bc79c13667940203add35fa13fac21f1 (diff) |
Merge pull request #235 from solidusio-contrib/kennyadsl/log-installments-errors-by-defaultHEADmaster
Log installments errors by default
6 files changed, 54 insertions, 4 deletions
diff --git a/app/jobs/solidus_subscriptions/process_installment_job.rb b/app/jobs/solidus_subscriptions/process_installment_job.rb index 53270e5..84d077a 100644 --- a/app/jobs/solidus_subscriptions/process_installment_job.rb +++ b/app/jobs/solidus_subscriptions/process_installment_job.rb @@ -7,7 +7,7 @@ module SolidusSubscriptions def perform(installment) Checkout.new(installment).process rescue StandardError => e - SolidusSubscriptions.configuration.processing_error_handler&.call(e) + SolidusSubscriptions.configuration.processing_error_handler&.call(e, installment) end end end diff --git a/lib/generators/solidus_subscriptions/install/templates/initializer.rb b/lib/generators/solidus_subscriptions/install/templates/initializer.rb index de1d081..7ca79c5 100644 --- a/lib/generators/solidus_subscriptions/install/templates/initializer.rb +++ b/lib/generators/solidus_subscriptions/install/templates/initializer.rb @@ -31,7 +31,9 @@ SolidusSubscriptions.configure do |config| # on an error tracking system. # Though not recommended due to the retry mechanisms built into this gem, the error can be # re-raised if the default retry behaviour is required in ActiveJob. - # config.processing_error_handler = nil + # + # By default, it only logs the error message using Rails.logger.error. + # config.processing_error_handler = SolidusSubscriptions::ProcessingErrorHandlers::RailsLogger # ========================================= Dispatchers ========================================== # diff --git a/lib/solidus_subscriptions/configuration.rb b/lib/solidus_subscriptions/configuration.rb index 9d6c3d2..350fabc 100644 --- a/lib/solidus_subscriptions/configuration.rb +++ b/lib/solidus_subscriptions/configuration.rb @@ -4,14 +4,14 @@ module SolidusSubscriptions class Configuration attr_accessor( :maximum_total_skips, :maximum_reprocessing_time, :churn_buster_account_id, - :churn_buster_api_key, :clear_past_installments, :processing_error_handler + :churn_buster_api_key, :clear_past_installments ) attr_writer( :success_dispatcher_class, :failure_dispatcher_class, :payment_failed_dispatcher_class, :out_of_stock_dispatcher, :maximum_successive_skips, :reprocessing_interval, :minimum_cancellation_notice, :processing_queue, :subscription_line_item_attributes, - :subscription_attributes, :subscribable_class, :order_creator_class + :subscription_attributes, :subscribable_class, :order_creator_class, :processing_error_handler ) def success_dispatcher_class @@ -34,6 +34,11 @@ module SolidusSubscriptions @out_of_stock_dispatcher_class.constantize end + def processing_error_handler + @processing_error_handler ||= 'SolidusSubscriptions::ProcessingErrorHandlers::RailsLogger' + @processing_error_handler.constantize + end + def maximum_successive_skips @maximum_successive_skips ||= 1 end diff --git a/lib/solidus_subscriptions/engine.rb b/lib/solidus_subscriptions/engine.rb index 0f274d8..2d361cf 100644 --- a/lib/solidus_subscriptions/engine.rb +++ b/lib/solidus_subscriptions/engine.rb @@ -6,6 +6,7 @@ require 'solidus_subscriptions' require 'solidus_subscriptions/permitted_attributes' require 'solidus_subscriptions/configuration' require 'solidus_subscriptions/processor' +require 'solidus_subscriptions/processing_error_handlers/rails_logger' module SolidusSubscriptions class Engine < Rails::Engine diff --git a/lib/solidus_subscriptions/processing_error_handlers/rails_logger.rb b/lib/solidus_subscriptions/processing_error_handlers/rails_logger.rb new file mode 100644 index 0000000..d6fcf7e --- /dev/null +++ b/lib/solidus_subscriptions/processing_error_handlers/rails_logger.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module SolidusSubscriptions + module ProcessingErrorHandlers + class RailsLogger + def self.call(exception, installment = nil) + new(exception, installment).call + end + + def initialize(exception, installment = nil) + @exception = exception + @installment = installment + end + + def call + Rails.logger.error("Error processing installment with ID=#{installment.id}:") if installment + Rails.logger.error(exception.message) + end + + private + + attr_reader :exception, :installment + end + end +end diff --git a/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb b/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb index bc33d0e..767d650 100644 --- a/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb +++ b/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb @@ -12,7 +12,24 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentJob do end context 'when handling #perform errors' do + it 'by default logs exception data without raising exceptions' do # rubocop:disable RSpec/MultipleExpectations + installment = build_stubbed(:installment) + checkout = instance_double(SolidusSubscriptions::Checkout).tap do |c| + allow(c).to receive(:process).and_raise('test error') + end + allow(SolidusSubscriptions::Checkout).to receive(:new).and_return(checkout) + allow(Rails.logger).to receive(:error) + + expect { + described_class.perform_now(installment) + }.not_to raise_error + + expect(Rails.logger).to have_received(:error).with("Error processing installment with ID=#{installment.id}:").ordered + expect(Rails.logger).to have_received(:error).with("test error").ordered + end + it 'swallows error when a proc is not configured' do + stub_config(processing_error_handler: nil ) checkout = instance_double(SolidusSubscriptions::Checkout).tap do |c| allow(c).to receive(:process).and_raise('test error') end |