Created
June 28, 2016 13:41
-
-
Save 907th/ad397c0c4a3a97d2436ee343c39a245f to your computer and use it in GitHub Desktop.
Exception helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MemberApi | |
module ExceptionHelpers | |
# Usage: | |
# render_validation_errors(model) unless model.valid? | |
# | |
# @param model [ActiveRecord::Base] | |
# | |
def render_validation_errors(model, status: :bad_request) | |
render json: { | |
full_messages: model.errors.full_messages, | |
invalid_attributes: model.errors.to_h | |
}, status: status | |
end | |
# Usage: | |
# begin | |
# # ... | |
# rescue SomeError => error | |
# render_error error | |
# end | |
# | |
# @param error [StandardError] | |
# | |
def render_error(error, status: :internal_server_error) | |
render json: { error: error.message }, status: status | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment