Skip to content

Make profile api errors translatable #565

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ en:
website: "must be a valid URL"
invitation:
email_address: "'%<value>s' is invalid"
school_student:
not_empty: "You must supply a %{field}"
2 changes: 1 addition & 1 deletion lib/concepts/school_student/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 2 additions & 7 deletions lib/concepts/school_student/create_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/concepts/school_student/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/profile_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/concepts/school_student/create_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, <a href="https://my.raspberrypi.org/password-help">need password help?</a>)', '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
Expand Down
4 changes: 2 additions & 2 deletions spec/concepts/school_student/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down