Skip to content

Commit

Permalink
remove unwanted rescue
Browse files Browse the repository at this point in the history
  • Loading branch information
krbhavith committed Dec 17, 2024
1 parent 139cfb4 commit 7b4929d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 272 deletions.
4 changes: 1 addition & 3 deletions app/jobs/bulk_zombie_url_uploader_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def report_results

def log_results
results = @uploader.results
Rails.logger.info "BulkZombieUrlUploaderJob: #{results.file_name}"
Rails.logger.info " #{results.total_count} URLs"
Rails.logger.info " #{results.error_count} errors"
Rails.logger.info(BulkZombieUrlUploaderJob: results.file_name, total_urls: results.total_count, errors: results.error_count)
end

def send_results_email
Expand Down
29 changes: 4 additions & 25 deletions app/services/bulk_zombie_url_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ def initialize_results
end

def process_upload
parse_csv.each { |row| process_row(row) }
rescue CSV::MalformedCSVError => e
handle_csv_error(e)
end

def parse_csv
csv = CSV.parse(File.read(@file_path), headers: true)
raise CSV::MalformedCSVError, "Missing required headers" unless %w[URL DOC_ID].all? { |col| csv.headers.include?(col) }
csv
rescue CSV::MalformedCSVError, ArgumentError => e
raise CSV::MalformedCSVError.new('CSV', "Malformed or invalid CSV: #{e.message}")
CSV.parse(File.read(@file_path), headers: true).each { |row| process_row(row) }
end

def process_row(row)
Expand All @@ -53,7 +43,7 @@ def process_row(row)
end

def handle_url_processing(url, document_id, row)
process_url_with_rescue(url, document_id)
process_url(url, document_id)
update_results
rescue StandardError => e
handle_processing_error(e, url, document_id, row)
Expand All @@ -69,26 +59,15 @@ def log_missing_document_id(row, url)
Rails.logger.error("Skipping row: #{row.inspect}. Document ID is mandatory.")
end

def handle_csv_error(error)
@results.add_error('Invalid CSV format', 'Entire file')
Rails.logger.error("Error parsing CSV: #{error.message}")
end

def log_upload_error(error)
error_message = "Failed to process bulk zombie URL document (file: #{@file_name})."
backtrace = error.backtrace ? error.backtrace.join("\n") : 'No backtrace available'
Rails.logger.error("#{error_message} Error: #{error.message}\n#{backtrace}")
Rails.logger.error(error_message, error)
end

def handle_processing_error(error, url, document_id, row)
key = url.presence || document_id
@results&.add_error(error.message, key)
backtrace = error.backtrace ? error.backtrace.join("\n") : 'No backtrace available'
Rails.logger.error("Failure to process bulk upload zombie URL row: #{row.inspect}\n#{error.message}\n#{backtrace}")
end

def process_url_with_rescue(url, document_id)
process_url(url, document_id)
Rails.logger.error('Failure to process bulk upload zombie URL row:', error)
end

def process_url(url, document_id)
Expand Down
8 changes: 7 additions & 1 deletion spec/jobs/bulk_zombie_url_uploader_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
expect(BulkZombieUrlUploader).to have_received(:new).with(filename, filepath)
expect(uploader).to have_received(:upload)
expect(BulkZombieUrlUploadResultsMailer).to have_received(:with).with(user:, results:)
expect(Rails.logger).to have_received(:info).with(/BulkZombieUrlUploaderJob:/)
expect(Rails.logger).to have_received(:info).with(
hash_including(
BulkZombieUrlUploaderJob: filename,
total_urls: 10,
errors: 2
)
)
end
end
end
Loading

0 comments on commit 7b4929d

Please sign in to comment.