diff --git a/lib/concepts/school_student/create.rb b/lib/concepts/school_student/create.rb index 4dc5a5209..2c8433706 100644 --- a/lib/concepts/school_student/create.rb +++ b/lib/concepts/school_student/create.rb @@ -4,13 +4,13 @@ module SchoolStudent class Create class << self def call(school:, school_student_params:, token:) - response = OperationResponse.new - response[:student_id] = create_student(school, school_student_params, token) - response + student_id = create_student(school, school_student_params, token) + OperationResponse[student_id:] + rescue ProfileApiClient::Student422Error => e + OperationResponse[error: e.to_s] rescue StandardError => e Sentry.capture_exception(e) - response[:error] = e.to_s - response + OperationResponse[error: e.to_s] end private diff --git a/spec/concepts/school_student/create_spec.rb b/spec/concepts/school_student/create_spec.rb index b2cca20fc..f150619ff 100644 --- a/spec/concepts/school_student/create_spec.rb +++ b/spec/concepts/school_student/create_spec.rb @@ -77,17 +77,28 @@ end end - context 'when the student cannot be created in profile api because of a 422 response' do - let(:error) { { 'message' => "something's up with the username" } } + context 'when Profile API rejects the student details' do + let(:error) do + { + 'errorCode' => 'isComplex', + 'message' => 'Password is too simple' + } + end let(:exception) { ProfileApiClient::Student422Error.new(error) } before do allow(ProfileApiClient).to receive(:create_school_student).and_raise(exception) + allow(Sentry).to receive(:capture_exception) end - it 'adds a useful error message' do + it 'returns the translatable error code' do response = described_class.call(school:, school_student_params:, token:) - expect(response[:error]).to eq("something's up with the username") + expect(response[:error]).to eq('isComplex') + end + + it 'does not send the expected validation error to Sentry' do + described_class.call(school:, school_student_params:, token:) + expect(Sentry).not_to have_received(:capture_exception) end end