Skip to content

Instantly share code, notes, and snippets.

@katpadi
Last active Aug 29, 2015
Embed
What would you like to do?
Exceptions Handler
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