Last active
August 29, 2015 14:09
-
-
Save katpadi/f4137f046f75e6998c7d to your computer and use it in GitHub Desktop.
Exceptions Handler
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 API | |
module V1 | |
module ExceptionsHandler | |
extend ActiveSupport::Concern | |
included do | |
rescue_from ActiveRecord::RecordNotFound do |e| | |
error_response(message: e.message, status: 404) | |
end | |
rescue_from CanCan::AccessDenied do |e| | |
error_response(message: e.message, status: 403) | |
end | |
# Example of a random exception | |
rescue_from KatPadiService::Client::ParamoreException do | |
error_response(message: 'You are not the exception', status: 501) | |
end | |
rescue_from Grape::Exceptions::ValidationErrors do |e| | |
error_response(message: e.message, status: 406) | |
end | |
# When all else fails... | |
rescue_from :all do |e| | |
Rails.logger.error "\n#{e.class.name} (#{e.message}):" | |
e.backtrace.each { |line| Rails.logger.error line } | |
error_response(message: 'Internal server error', status: 500) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment