Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize error handling #19927

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,10 @@
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
Expand All @@ -82,7 +50,7 @@
private

if Flipper.enabled?(:champva_multiple_stamp_retry, @current_user)
def handle_file_uploads(form_id, parsed_form_data)

Check failure on line 53 in modules/ivc_champva/app/controllers/ivc_champva/v1/uploads_controller.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Metrics/MethodLength: Method has too many lines. [27/20]
attempt = 0
max_attempts = 1

Expand All @@ -95,13 +63,21 @@
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

Expand Down
Loading