diff --git a/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider.rb b/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider.rb index 1237d8f2b53..89028203bc4 100644 --- a/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider.rb +++ b/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider.rb @@ -8,6 +8,14 @@ class LighthouseSupplementalDocumentUploadProvider STATSD_PROVIDER_METRIC = 'lighthouse_supplemental_document_upload_provider' + # Custom exceptions for Lighthouse API non-200 responses + # thrown in lib/lighthouse/service_exception.rb + LIGHTHOUSE_RESPONSE_EXCEPTION_CLASSES = [ + *Lighthouse::ServiceException::ERROR_MAP.values, + Common::Exceptions::Timeout, + Common::Exceptions::ServiceError + ].freeze + # Maps VA's internal Document Types to the correct document_type attribute for a Lighthouse526DocumentUpload polling # record. We need this to create a valid polling record POLLING_DOCUMENT_TYPES = { @@ -61,7 +69,14 @@ def validate_upload_document(lighthouse_document) # @param file_body [String] def submit_upload_document(lighthouse_document, file_body) log_upload_attempt - api_response = BenefitsDocuments::Form526::UploadSupplementalDocumentService.call(file_body, lighthouse_document) + + begin + api_response = BenefitsDocuments::Form526::UploadSupplementalDocumentService.call(file_body, lighthouse_document) + rescue *LIGHTHOUSE_RESPONSE_EXCEPTION_CLASSES => e + log_upload_failure(e) + raise e + end + handle_lighthouse_response(api_response) end @@ -114,20 +129,33 @@ def log_upload_success(lighthouse_request_id) StatsD.increment("#{@statsd_metric_prefix}.#{STATSD_PROVIDER_METRIC}.#{STATSD_SUCCESS_METRIC}") end - # For logging an error response from the Lighthouse Benefits Document API - # - # @param lighthouse_error_response [Hash] parsed JSON response from the Lighthouse API - # this will be an array of errors - def log_upload_failure(lighthouse_error_response) + def log_upload_failure(exception) + # puts "exception" + # puts exception + # puts "exception class" + # puts exception.class + + # puts "exception message" + # puts exception.message + + + + + StatsD.increment("#{@statsd_metric_prefix}.#{STATSD_PROVIDER_METRIC}.#{STATSD_FAILED_METRIC}") + Rails.logger.error( 'LighthouseSupplementalDocumentUploadProvider upload failed', { **base_logging_info, - lighthouse_error_response: + error_info: exception.to_s } ) - StatsD.increment("#{@statsd_metric_prefix}.#{STATSD_PROVIDER_METRIC}.#{STATSD_FAILED_METRIC}") + + # puts "logging info" + # puts exception.to_s + # puts { **base_logging_info, error_info: exception.to_s + end # Processes the response from Lighthouse and logs accordingly. If the upload is successful, creates @@ -141,8 +169,6 @@ def handle_lighthouse_response(api_response) lighthouse_request_id = response_body.dig('data', 'requestId') create_lighthouse_polling_record(lighthouse_request_id) log_upload_success(lighthouse_request_id) - else - log_upload_failure(response_body) end end diff --git a/lib/lighthouse/benefits_documents/form526/upload_supplemental_document_service.rb b/lib/lighthouse/benefits_documents/form526/upload_supplemental_document_service.rb index b950b618bee..d38509f4408 100644 --- a/lib/lighthouse/benefits_documents/form526/upload_supplemental_document_service.rb +++ b/lib/lighthouse/benefits_documents/form526/upload_supplemental_document_service.rb @@ -38,6 +38,7 @@ def call e, self.class.to_s.underscore, nil, BenefitsDocuments::Configuration::DOCUMENTS_PATH ) + # Don't need to re-raise? raise e end end diff --git a/spec/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider_spec.rb b/spec/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider_spec.rb index a1091495e9b..1d1d5496a07 100644 --- a/spec/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider_spec.rb +++ b/spec/lib/disability_compensation/providers/document_upload/lighthouse_supplemental_document_upload_provider_spec.rb @@ -207,55 +207,59 @@ end end - context 'when we get a non-200 response from Lighthouse' do - let(:error_response_body) do - # From vcr_cassettes/lighthouse/benefits_claims/documents/lighthouse_form_526_document_upload_400.yml - { - 'errors' => [ - { - 'detail' => 'Something broke', - 'status' => 400, - 'title' => 'Bad Request', - 'instance' => Faker::Internet.uuid - } - ] - } - end - + # See lib/lighthouse/service_exception.rb + context 'when a Lighthouse::ServiceException error is raised' do before do # Skip upload attempt logging allow(provider).to receive(:log_upload_attempt) + end - allow(BenefitsDocuments::Form526::UploadSupplementalDocumentService).to receive(:call) - .with(file_body, lighthouse_document) - .and_return(faraday_response) + RSpec.shared_examples 'log Lighthouse response exception' do |exception_class| + it 'increments a StatsD failure metric, logs the error metadata and re-raises the error' do - allow(faraday_response).to receive(:body).and_return(error_response_body) - end - it 'logs to the Rails logger' do - expect(Rails.logger).to receive(:error).with( - 'LighthouseSupplementalDocumentUploadProvider upload failed', - { - class: 'LighthouseSupplementalDocumentUploadProvider', - submitted_claim_id: submission.submitted_claim_id, - submission_id: submission.id, - user_uuid: submission.user_uuid, - va_document_type_code: va_document_type, - primary_form: 'Form526', - lighthouse_error_response: error_response_body - } - ) + Common::Exceptions::Timeout.new(errors: [{ title: error.class, detail: error.message }]) - provider.submit_upload_document(lighthouse_document, file_body) + + errors_info = [{title: exception.class, detail: ''}] + + + + error_info = { title: exception_class, detail: 'error message' } + exception = exception_class.new(error_info) + + allow(BenefitsDocuments::Form526::UploadSupplementalDocumentService).to receive(:call) + .and_raise(exception) + + expect(StatsD).to receive(:increment).with( + 'my_stats_metric_prefix.lighthouse_supplemental_document_upload_provider.upload_failure' + ) + expect(Rails.logger).to receive(:error).with( + 'LighthouseSupplementalDocumentUploadProvider upload failed', + { + class: 'LighthouseSupplementalDocumentUploadProvider', + submitted_claim_id: submission.submitted_claim_id, + submission_id: submission.id, + user_uuid: submission.user_uuid, + va_document_type_code: va_document_type, + primary_form: 'Form526', + error_info: error_info.to_s + } + ) + + expect { provider.submit_upload_document(lighthouse_document, file_body) }.to raise_error(exception) + end end - it 'increments a StatsD metric' do - expect(StatsD).to receive(:increment).with( - 'my_stats_metric_prefix.lighthouse_supplemental_document_upload_provider.upload_failure' - ) + describe 'service exceptions' do + error_values = Lighthouse::ServiceException::ERROR_MAP.values - provider.submit_upload_document(lighthouse_document, file_body) + error_values.each do |exception| + it_behaves_like 'log Lighthouse response exception', exception + end + + it_behaves_like 'log Lighthouse response exception', Common::Exceptions::Timeout + it_behaves_like 'log Lighthouse response exception', Common::Exceptions::ServiceError end end