Created
October 21, 2018 19:25
-
-
Save pascalesdedy/b8b68701fc22633560f938961d03cd1a to your computer and use it in GitHub Desktop.
app/controller/concerns files - Simple Rails API server - TDD
This file contains hidden or 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
#app/controller/concerns/response.rb | |
module Response | |
def json_response(object, status = :ok) | |
render json: object, status: status | |
end | |
end | |
#app/controller/concerns/exception_handler.rb | |
module ExceptionHandler | |
# provides the more graceful `included` method | |
extend ActiveSupport::Concern | |
included do | |
rescue_from ActiveRecord::RecordNotFound do |e| | |
json_response({ message: e.message }, :not_found) | |
end | |
rescue_from ActiveRecord::RecordInvalid do |e| | |
json_response({ message: e.message }, :unprocessable_entity) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment