diff --git a/lib/concepts/school_student/create.rb b/lib/concepts/school_student/create.rb index b6e4fb618..b72868b77 100644 --- a/lib/concepts/school_student/create.rb +++ b/lib/concepts/school_student/create.rb @@ -7,10 +7,6 @@ def call(school:, school_student_params:, token:) response = OperationResponse.new response[:student_id] = create_student(school, school_student_params, token) response - rescue ProfileApiClient::Student422Error => e - Sentry.capture_exception(e) - response[:error] = "Error creating school student: #{e.error}" - response rescue StandardError => e Sentry.capture_exception(e) response[:error] = "Error creating school student: #{e}" diff --git a/lib/profile_api_client.rb b/lib/profile_api_client.rb index 6bcdc466f..9363a3929 100644 --- a/lib/profile_api_client.rb +++ b/lib/profile_api_client.rb @@ -13,21 +13,9 @@ class ProfileApiClient class Error < StandardError; end class Student422Error < Error - DEFAULT_ERROR = 'unknown error' - ERRORS = { - 'ERR_USER_EXISTS' => 'username has already been taken', - 'ERR_INVALID' => 'unknown validation error', - 'ERR_INVALID_PASSWORD' => 'password is invalid', - 'ERR_UNKNOWN' => DEFAULT_ERROR - }.freeze - - attr_reader :username, :error - def initialize(error) - @username = error['username'] - @error = ERRORS.fetch(error['error'], DEFAULT_ERROR) - - super "Student not saved in Profile API (status code 422, username '#{@username}', error '#{@error}')" + @message = error['message'] + super @message end end @@ -109,7 +97,7 @@ def create_school_student(token:, username:, password:, name:, school_id:) raise UnexpectedResponse, response unless response.status == 201 response.body.deep_symbolize_keys - rescue Faraday::UnprocessableEntityError => e + rescue Faraday::BadRequestError => e raise Student422Error, JSON.parse(e.response_body)['errors'].first end @@ -127,7 +115,7 @@ def update_school_student(token:, school_id:, student_id:, name: nil, username: raise UnexpectedResponse, response unless response.status == 200 Student.new(**response.body) - rescue Faraday::UnprocessableEntityError => e + rescue Faraday::BadRequestError => e raise Student422Error, JSON.parse(e.response_body)['errors'].first end diff --git a/spec/concepts/school_student/create_spec.rb b/spec/concepts/school_student/create_spec.rb index f7419c100..6d4b2d082 100644 --- a/spec/concepts/school_student/create_spec.rb +++ b/spec/concepts/school_student/create_spec.rb @@ -86,7 +86,7 @@ end context 'when the student cannot be created in profile api because of a 422 response' do - let(:error) { { 'username' => 'username', 'error' => 'ERR_USER_EXISTS' } } + let(:error) { { 'message' => "something's up with the username" } } let(:exception) { ProfileApiClient::Student422Error.new(error) } before do @@ -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: username has already been taken') + expect(response[:error]).to eq("Error creating school student: something's up with the username") end end diff --git a/spec/lib/profile_api_client_spec.rb b/spec/lib/profile_api_client_spec.rb index 48ed7ce3e..cfc40f630 100644 --- a/spec/lib/profile_api_client_spec.rb +++ b/spec/lib/profile_api_client_spec.rb @@ -16,18 +16,10 @@ subject(:exception) { described_class.new(error) } let(:error_code) { 'ERR_USER_EXISTS' } - let(:error) { { 'username' => 'username', 'error' => error_code } } + let(:error) { { 'message' => "Something's wrong with the password" } } - it 'includes status code, username and translated error code in the message' do - expect(exception.message).to eq("Student not saved in Profile API (status code 422, username 'username', error 'username has already been taken')") - end - - context "when the error isn't recognised" do - let(:error_code) { 'unrecognised-code' } - - it 'includes a default error message' do - expect(exception.message).to match(/error 'unknown error'/) - end + it 'includes the message from the error' do + expect(exception.message).to eq("Something's wrong with the password") end end @@ -379,13 +371,13 @@ def delete_safeguarding_flag expect(create_school_student).to eq(response) end - it 'raises 422 exception if 422 status code is returned' do - response = { errors: [username: 'username', error: 'ERR_USER_EXISTS'] } + it 'raises 422 exception with the relevant message if 400 status code is returned' do + response = { errors: [message: 'The password is well dodgy'] } stub_request(:post, create_students_url) - .to_return(status: 422, body: response.to_json, headers: { 'content-type' => 'application/json' }) + .to_return(status: 400, body: response.to_json, headers: { 'content-type' => 'application/json' }) expect { create_school_student }.to raise_error(ProfileApiClient::Student422Error) - .with_message("Student not saved in Profile API (status code 422, username 'username', error 'username has already been taken')") + .with_message('The password is well dodgy') end it 'raises exception if anything other that 201 status code is returned' do @@ -548,13 +540,13 @@ def list_school_students expect(update_school_student).to eq(expected) end - it 'raises 422 exception if 422 status code is returned' do - response = { errors: [username: 'username', error: 'ERR_USER_EXISTS'] } + it 'raises 422 exception with the relevant message if 400 status code is returned' do + response = { errors: [message: 'The username is well dodgy'] } stub_request(:patch, update_student_url) - .to_return(status: 422, body: response.to_json, headers: { 'content-type' => 'application/json' }) + .to_return(status: 400, body: response.to_json, headers: { 'content-type' => 'application/json' }) expect { update_school_student }.to raise_error(ProfileApiClient::Student422Error) - .with_message("Student not saved in Profile API (status code 422, username 'username', error 'username has already been taken')") + .with_message('The username is well dodgy') end it 'raises exception if anything other that 200 status code is returned' do