diff --git a/config/locales/en.yml b/config/locales/en.yml index d2cf7ba91..33eae4415 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -16,5 +16,3 @@ en: website: "must be a valid URL" invitation: email_address: "'%s' is invalid" - school_student: - not_empty: "You must supply a %{field}" diff --git a/lib/concepts/school_student/create.rb b/lib/concepts/school_student/create.rb index 22ba9b1fc..0d4d3925c 100644 --- a/lib/concepts/school_student/create.rb +++ b/lib/concepts/school_student/create.rb @@ -9,7 +9,7 @@ def call(school:, school_student_params:, token:) response rescue StandardError => e Sentry.capture_exception(e) - response[:error] = "Error creating school student: #{e}" + response[:error] = e.to_s response end diff --git a/lib/concepts/school_student/create_batch.rb b/lib/concepts/school_student/create_batch.rb index e4074af2e..a1048e75f 100644 --- a/lib/concepts/school_student/create_batch.rb +++ b/lib/concepts/school_student/create_batch.rb @@ -30,7 +30,7 @@ def call(school:, school_students_params:, token:, user_id:) response rescue StandardError => e Sentry.capture_exception(e) - response[:error] = "Error creating school students: #{e}" + response[:error] = e.to_s response[:error_type] = :standard_error response end @@ -65,14 +65,9 @@ def decrypt_students(students) def handle_student422_error(errors) formatted_errors = errors.each_with_object({}) do |error, hash| username = error['username'] || error['path'] - field = error['path'].split('.').last hash[username] ||= [] - hash[username] << I18n.t( - "validations.school_student.#{error['errorCode'].underscore}", - field:, - default: error['message'] - ) + hash[username] << (error['errorCode'] || error['message']) # Ensure uniqueness to avoid repeat errors with duplicate usernames hash[username] = hash[username].uniq diff --git a/lib/concepts/school_student/update.rb b/lib/concepts/school_student/update.rb index 2a5b8742f..67f2acdd4 100644 --- a/lib/concepts/school_student/update.rb +++ b/lib/concepts/school_student/update.rb @@ -9,7 +9,7 @@ def call(school:, student_id:, school_student_params:, token:) response rescue StandardError => e Sentry.capture_exception(e) - response[:error] = "Error updating school student: #{e}" + response[:error] = e.to_s response end diff --git a/lib/profile_api_client.rb b/lib/profile_api_client.rb index 4d9892313..f3af52362 100644 --- a/lib/profile_api_client.rb +++ b/lib/profile_api_client.rb @@ -18,7 +18,7 @@ class Student422Error < StandardError def initialize(errors) @errors = errors if errors.is_a?(Hash) - super(errors['message']) + super(errors['errorCode'] || errors['message']) else super() end diff --git a/spec/concepts/school_student/create_batch_spec.rb b/spec/concepts/school_student/create_batch_spec.rb index 4ec03ec04..436ffe46e 100644 --- a/spec/concepts/school_student/create_batch_spec.rb +++ b/spec/concepts/school_student/create_batch_spec.rb @@ -92,7 +92,7 @@ it 'returns the error message in the operation response' do response = described_class.call(school:, school_students_params:, token:, user_id:) error_message = response[:error] - expect(error_message).to match(/Error creating school students: Decryption failed: iv must be 16 bytes/) + expect(error_message).to match(/Decryption failed: iv must be 16 bytes/) end it 'sent the exception to Sentry' do @@ -106,10 +106,10 @@ stub_profile_api_create_school_students_validation_error end - it 'returns the expected formatted errors' do + it 'returns the expected error codes' do response = described_class.call(school:, school_students_params:, token:, user_id:) expect(response[:error]).to eq( - { 'student-to-create' => ['Username must be unique in the batch data', 'Password is too simple (it should not be easily guessable, need password help?)', 'You must supply a name'], 'another-student-to-create-2' => ['Password must be at least 8 characters', 'You must supply a name'] } + { 'student-to-create' => %w[isUniqueInBatch isComplex notEmpty], 'another-student-to-create-2' => %w[minLength notEmpty] } ) end end diff --git a/spec/concepts/school_student/create_spec.rb b/spec/concepts/school_student/create_spec.rb index 17ef3807b..4d2d87c5a 100644 --- a/spec/concepts/school_student/create_spec.rb +++ b/spec/concepts/school_student/create_spec.rb @@ -95,7 +95,7 @@ it 'adds a useful error message' do response = described_class.call(school:, school_student_params:, token:) - expect(response[:error]).to eq("Error creating school student: something's up with the username") + expect(response[:error]).to eq("something's up with the username") end end @@ -107,7 +107,7 @@ it 'adds a useful error message' do response = described_class.call(school:, school_student_params:, token:) - expect(response[:error]).to eq('Error creating school student: Student not created in Profile API (status code 401)') + expect(response[:error]).to eq('Student not created in Profile API (status code 401)') end end end diff --git a/spec/features/school_student/creating_a_batch_of_school_students_spec.rb b/spec/features/school_student/creating_a_batch_of_school_students_spec.rb index eabb536b1..9778f3d96 100644 --- a/spec/features/school_student/creating_a_batch_of_school_students_spec.rb +++ b/spec/features/school_student/creating_a_batch_of_school_students_spec.rb @@ -101,7 +101,7 @@ it 'responds 422 Unprocessable Entity with a suitable message when params are invalid' do post("/api/schools/#{school.id}/students/batch", headers:, params: bad_params) expect(response).to have_http_status(:unprocessable_entity) - expect(response.body).to include('Error creating school students: Decryption failed: iv must be 16 bytes') + expect(response.body).to include('Decryption failed: iv must be 16 bytes') end # rubocop:enable RSpec/MultipleExpectations @@ -110,7 +110,7 @@ stub_profile_api_create_school_students_validation_error post("/api/schools/#{school.id}/students/batch", headers:, params:) expect(response).to have_http_status(:unprocessable_entity) - expect(response.body).to eq('{"error":{"student-to-create":["Username must be unique in the batch data","Password is too simple (it should not be easily guessable, \\u003ca href=\"https://my.raspberrypi.org/password-help\"\\u003eneed password help?\\u003c/a\\u003e)","You must supply a name"],"another-student-to-create-2":["Password must be at least 8 characters","You must supply a name"]},"error_type":"validation_error"}') + expect(response.body).to eq('{"error":{"student-to-create":["isUniqueInBatch","isComplex","notEmpty"],"another-student-to-create-2":["minLength","notEmpty"]},"error_type":"validation_error"}') end # rubocop:enable RSpec/MultipleExpectations