diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index cdc99e0..ff818f0 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -6,7 +6,11 @@ class ApiController < ApplicationController def notify return head 400 if params[:to]&.match(/psnator/) - @notification = Notification.from_params(params) + begin + @notification = Notification.from_params(params) + rescue => e + return render json: {error: e.message}, status: 400 + end render json: @notification end end diff --git a/spec/controllers/api/api_controller_spec.rb b/spec/controllers/api/api_controller_spec.rb index 3e980d7..7196b6d 100644 --- a/spec/controllers/api/api_controller_spec.rb +++ b/spec/controllers/api/api_controller_spec.rb @@ -27,6 +27,16 @@ } expect(response.code).to eq('400') end + it 'handles invalid email addresses' do + post :notify, params: + { + to: 'not a valid email address', + subject: 'hello', + body: 'goodbye' + } + expect(response.code).to eq('400') + expect(response.body).to match('email(.*)invalid') + end it 'supports multiple recipients' do stub_request(:post, 'https://api.sendgrid.com/v3/mail/send') .with(