Skip to content

Instantly share code, notes, and snippets.

@katpadi
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katpadi/f4137f046f75e6998c7d to your computer and use it in GitHub Desktop.
Save katpadi/f4137f046f75e6998c7d to your computer and use it in GitHub Desktop.
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