Module: Customs::RescueTraffic

Defined in:
lib/customs/rescue_traffic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/customs/rescue_traffic.rb', line 4

def self.included base
  base.rescue_from CanCan::AccessDenied, with: proc{|e| rescue_exception_with e, :access_denied }

  base.rescue_from ActionController::UnknownFormat, with: proc{|e| rescue_exception_with e, :not_acceptable } if defined? ActionController::UnknownFormat

  base.rescue_from ActiveRecord::RecordNotFound, with: proc{|e| rescue_exception_with e, :not_found }     if defined? ActiveRecord::RecordNotFound
  base.rescue_from ActiveRecord::RecordInvalid , with: proc{|e| rescue_exception_with e, :unprocessable } if defined? ActiveRecord::RecordInvalid

  base.rescue_from Mongoid::Errors::DocumentNotFound, with: proc{|e| rescue_exception_with e, :not_found }     if defined? Mongoid::Errors
  base.rescue_from Mongoid::Errors::Validations     , with: proc{|e| rescue_exception_with e, :unprocessable } if defined? Mongoid::Errors
end

Instance Method Details

#rescue_exception_with(exception, status_method) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/customs/rescue_traffic.rb', line 16

def rescue_exception_with exception, status_method
  if respond_to?(:logger) && logger
    logger.info "[Customs] Rescuing from #{ exception.class } with :#{ status_method }"
  end

  send status_method
end