FEATURE: improve API error reporting for invalid records

This commit is contained in:
Sam 2018-08-21 11:54:34 +10:00
parent f939bdbf44
commit 2d96160192
2 changed files with 14 additions and 0 deletions

View File

@ -129,6 +129,14 @@ class ApplicationController < ActionController::Base
)
end
rescue_from ActiveRecord::RecordInvalid do |e|
if request.format && request.format.json?
render_json_error e, type: :record_invalid, status: 422
else
raise e
end
end
# If they hit the rate limiter
rescue_from RateLimiter::LimitExceeded do |e|
render_rate_limit_error(e)

View File

@ -130,6 +130,12 @@ describe UsersEmailController do
expect(response.status).to eq(400)
end
it 'raises an error without an invalid email' do
put "/u/#{user.username}/preferences/email.json", params: { email: "sam@not-email.com'" }
expect(response.status).to eq(422)
expect(response.body).to include("email is invalid")
end
it "raises an error if you can't edit the user's email" do
Guardian.any_instance.expects(:can_edit_email?).with(user).returns(false)