module SimpleErrors::Rescue

A mixin for ApplicationController which rescues from common errors. If you have specific ones you want to rescue with a 404, call the class method rescue_with_not_found, passing one or more error classes

Public Instance Methods

before_rescue(&block) click to toggle source
# File lib/simple_errors/rescue.rb, line 41
def before_rescue(&block)
  @@before_rescue = block
end
call_before_rescue_block() click to toggle source
# File lib/simple_errors/rescue.rb, line 74
def call_before_rescue_block
  if defined?(@@before_rescue) && @@before_rescue.is_a?(Proc)
    instance_eval(&@@before_rescue)
  end
end
render_error(exception = nil) click to toggle source
# File lib/simple_errors/rescue.rb, line 61
def render_error(exception = nil)
  call_before_rescue_block
  @exception = exception
  respond_to do |format|
    format.html do
      render 'errors/500', status: 500, layout: 'layouts/error'
    end
    format.all do
      render nothing: true, status: 500
    end
  end
end
render_not_found(exception = nil) click to toggle source
# File lib/simple_errors/rescue.rb, line 48
def render_not_found(exception = nil)
  call_before_rescue_block
  @exception = exception
  respond_to do |format|
    format.html do
      render 'errors/404', status: 404, layout: "layouts/error"
    end
    format.all do
      render nothing: true, status: 404
    end
  end
end
rescue_with_not_found(*klasses) click to toggle source
# File lib/simple_errors/rescue.rb, line 35
def rescue_with_not_found(*klasses)
  @@rescue_with_not_found_from ||= []
  @@rescue_with_not_found_from << klasses
  @@rescue_with_not_found_from.flatten!
end