From bfeb9bab110ea89bd7c2103997692716199fc0d4 Mon Sep 17 00:00:00 2001 From: Don Shin Date: Tue, 17 Dec 2024 09:50:27 -0600 Subject: [PATCH] optimize error handling --- .../ivc_champva/v1/uploads_controller.rb | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/modules/ivc_champva/app/controllers/ivc_champva/v1/uploads_controller.rb b/modules/ivc_champva/app/controllers/ivc_champva/v1/uploads_controller.rb index 25eb41aca09..20a77e6d410 100644 --- a/modules/ivc_champva/app/controllers/ivc_champva/v1/uploads_controller.rb +++ b/modules/ivc_champva/app/controllers/ivc_champva/v1/uploads_controller.rb @@ -36,42 +36,10 @@ def submit render json: { error_message: "Error: #{e.message}" }, status: :internal_server_error end - # Modified from claim_documents_controller.rb: - def unlock_file(file, file_password) - return file unless File.extname(file) == '.pdf' && file_password - - pdftk = PdfForms.new(Settings.binaries.pdftk) - tmpf = Tempfile.new(['decrypted_form_attachment', '.pdf']) - - begin - pdftk.call_pdftk(file.tempfile.path, 'input_pw', file_password, 'output', tmpf.path) - rescue PdfForms::PdftkError => e - file_regex = %r{/(?:\w+/)*[\w-]+\.pdf\b} - password_regex = /(input_pw).*?(output)/ - sanitized_message = e.message.gsub(file_regex, '[FILTERED FILENAME]').gsub(password_regex, '\1 [FILTERED] \2') - log_message_to_sentry(sanitized_message, 'warn') - raise Common::Exceptions::UnprocessableEntity.new( - detail: I18n.t('errors.messages.uploads.pdf.incorrect_password'), - source: 'IvcChampva::V1::UploadsController' - ) - end - - file.tempfile.unlink - file.tempfile = tmpf - file - end - def submit_supporting_documents if %w[10-10D 10-7959C 10-7959F-2 10-7959A].include?(params[:form_id]) attachment = PersistentAttachments::MilitaryRecords.new(form_id: params[:form_id]) - - if Flipper.enabled?(:champva_pdf_decrypt, @current_user) - unlocked = unlock_file(params['file'], params['password']) - attachment.file = params['password'] ? unlocked : params['file'] - else - attachment.file = params['file'] - end - + attachment.file = params['file'] raise Common::Exceptions::ValidationErrors, attachment unless attachment.valid? attachment.save @@ -95,13 +63,21 @@ def handle_file_uploads(form_id, parsed_form_data) error_message_downcase = e.message.downcase Rails.logger.error "Error handling file uploads (attempt #{attempt}): #{e.message}" - if error_message_downcase.include?('failed to generate stamped file') || - (error_message_downcase.include?('unable to find file') && attempt <= max_attempts) + error_conditions = [ + 'failed to generate', + 'no such file', + 'an error occurred while verifying stamp:', + 'unable to find file' + ] + + if error_conditions.any? do |condition| + error_message_downcase.include?(condition) + end && attempt <= max_attempts Rails.logger.error 'Retrying in 1 seconds...' sleep 1 retry else - return [[], 'Error handling file uploads'] + return [[], 'no retries needed'] end end