Skip to content

Commit f36e3a3

Browse files
committed
Handle 4 kbyte error cap for upload materias
1 parent e3819e8 commit f36e3a3

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

app/controllers/admin/commercials_controller.rb

+23-10
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,35 @@ def render_commercial
5858
# EventID, Title, URL
5959
def mass_upload
6060
errors = Commercial.read_file(params[:file]) if params[:file]
61-
61+
6262
if !params[:file]
6363
flash[:error] = 'Empty file detected while adding materials to Event'
64-
elsif errors.all? { |_k, v| v.blank? }
65-
flash[:notice] = 'Successfully added materials.'
66-
else
67-
errors_text = ''
68-
errors_text += 'Unable to find event with ID: ' + errors[:no_event].join(', ') + '. ' if errors[:no_event].any?
69-
if errors[:validation_errors].any?
70-
errors_text += 'There were some errors: ' + errors[:validation_errors].join('. ')
64+
elsif errors.present?
65+
errors_text = aggregate_errors(errors)
66+
if errors_text.length > 4000 # If the error string exceeds 4 KB
67+
flash[:error] = 'There are too many errors to display. Please check your CSV file for issues.'
68+
else
69+
flash[:error] = errors_text
7170
end
72-
73-
flash[:error] = errors_text
71+
else
72+
flash[:notice] = 'Successfully added materials.'
7473
end
7574
redirect_back(fallback_location: root_path)
7675
end
76+
77+
private
78+
79+
# Aggregate errors and ensure that they do not exceed 4 KB in total size
80+
def aggregate_errors(errors)
81+
errors_text = ''
82+
if errors[:no_event].any?
83+
errors_text += 'Unable to find events with IDs: ' + errors[:no_event].join(', ') + '. '
84+
end
85+
if errors[:validation_errors].any?
86+
errors_text += 'Validation errors: ' + errors[:validation_errors].join('. ')
87+
end
88+
errors_text
89+
end
7790

7891
private
7992

0 commit comments

Comments
 (0)