Module: HTTPStatus

Defined in:
lib/http_status_exceptions.rb

Defined Under Namespace

Classes: Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Creates all the exception classes based on Rails’s list of available status code and registers the exception handler using the rescue_from method.



34
35
36
37
38
39
40
# File 'lib/http_status_exceptions.rb', line 34

def self.included(base)
  ActionController::StatusCodes::STATUS_CODES.each do |code, name|
    const_set(name.to_s.gsub(/[^A-Za-z]/, '').camelize, Class.new(HTTPStatus::Base)) if code >= 400
  end
  
  base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception)
end

Instance Method Details

#http_status_exception(exception) ⇒ Object

The default handler for raised HTTP status exceptions. It will render a template if available, or respond with an empty response with the HTTP status correspodning to the exception.



45
46
47
48
49
50
51
52
# File 'lib/http_status_exceptions.rb', line 45

def http_status_exception(exception)
  @exception = exception
  render_options = {:template => exception.template, :status => exception.status}
  render_options[:layout] = exception.template_layout if exception.template_layout
  render(render_options)
rescue ActionView::MissingTemplate
  head(exception.status)
end