Sha256: 0515d99886c1c8c458a8c9f9c74e669980e43114f87c4e3afa60c891bb8303c1

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

module ActionController::Error

  # I don't know why we have to pack the parameters into an array.
  class Exception < RuntimeError
    attr_reader :code, :text
    
    def initialize(args)
      @code, @text = *args
    end
  end
  
  def error(code, text=nil)
    raise Exception, [ code, text ]
  end
  
  def self.included(base)
    base.rescue_from ActionController::Error::Exception, :with => :rescue_custom_exceptions
    base.rescue_from ActionController::Error::Exception, :with => :rescue_custom_exceptions
    base.rescue_from ActionController::RoutingError, :with => :rescue_404
    base.rescue_from ActionController::UnknownAction, :with => :rescue_404
  end

  private

  def rescue_404
    @text = "You may have mistyped the address or the page may have moved."
    render :layout => "layouts/404", :text => "", :status => 404
  end
  
  def rescue_custom_exceptions(e = 404)
    if e.code == 404
      @text = e.text || "You may have mistyped the address or the page may have moved."
      render :layout => "layouts/404", :text => "", :status => 404
    elsif e.text.blank?
      render(:status => code, :nothing => true)
    else
      render(:status => code, :text => e.text.to_s, :content_type => "text/plain")
    end
  end
end

class ActionController::Base
  include ActionController::Error
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vex-0.4.4 lib/vex/action_controller/error.rb
vex-0.4.2 lib/vex/action_controller/error.rb
vex-0.3.3 lib/vex/action_controller/error.rb
vex-0.2.9 lib/vex/action_controller/error.rb
vex-0.2.8 lib/vex/action_controller/error.rb
vex-0.2.7 lib/vex/action_controller/error.rb
vex-0.2.6 lib/vex/action_controller/error.rb