Sha256: 98339e65ca70e085e06aa1c29c64a66a8e92c5e11c706decd861316c5ef59e1a

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 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

  def to_s
    "#{super}: #{@text}"
  end

  def inspect
    "#{super}: #{@text.inspect}"
  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

1 entries across 1 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/action_controller/error.rb