From c9e26197b0abbe50b029c368017243314d572884 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Mon, 12 Jul 2021 15:29:53 +0200 Subject: Logs an error in the Rails logs when installment processing fails At the moment, by default, all installment prceissing errors are swallowed and there's no way for a developer to understand what's happening. Of course they can create a custom handler with the processing_error_handler configuration provided but usually, when the first errors happen, it's too late and those errors are lost. We are not raising errors of this job because if there's a retry mechanism in place for Active Job, the installment will be reprocessed twice. Once by Active Job and once by the installment retry mechanism already provided by the extension. Logging to Rails.error is a middle-ground that allows to intercept the message of the exception. Still, creating a custom handler based on the bug tracker/exception handler used is the suggested option here. --- .../solidus_subscriptions/process_installment_job_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'spec/jobs') diff --git a/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb b/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb index bc33d0e..7211cfe 100644 --- a/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb +++ b/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb @@ -12,7 +12,22 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentJob do end context 'when handling #perform errors' do + it 'by default logs exception data without raising exceptions' do + 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(build_stubbed(:installment)) + }.not_to raise_error + + 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 -- cgit v1.2.3 From 0317e228bc79c13667940203add35fa13fac21f1 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Mon, 12 Jul 2021 17:04:48 +0200 Subject: Pass installment to the processing error handler This way we can provide more flexibility on things that can be done when an error occurs. In the provided RailsLogger handler, we'll also print the ID of the installment. --- spec/jobs/solidus_subscriptions/process_installment_job_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'spec/jobs') diff --git a/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb b/spec/jobs/solidus_subscriptions/process_installment_job_spec.rb index 7211cfe..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,8 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentJob do end context 'when handling #perform errors' do - it 'by default logs exception data without raising exceptions' 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 @@ -20,9 +21,10 @@ RSpec.describe SolidusSubscriptions::ProcessInstallmentJob do allow(Rails.logger).to receive(:error) expect { - described_class.perform_now(build_stubbed(:installment)) + 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 -- cgit v1.2.3