Skip to content
Fluxx edited this page Dec 21, 2010 · 5 revisions

This plugin consists of two parts, the HTTPStatus exception classes and the default exception handler. Both of these can be changed easily to fit your own needs.

Changing the exception handler

This plugin uses rescue_from to catch all HTTPStatus exceptions thrown in controllers and simply calls the http_status_exception method to handle it. You can reimplement this method in your application controller if you wish to customize the handling of these exceptions.

This is the default implementation of this method:

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

Altering HTTPStatus::Base

All exception classes are automatically generated and subclassed from HTTPStatus::Base. You can reopen this class to change its functionality, which in turn will then be available to all HTTPStatus::Base classes. This can be done by adding a file to your config/initializations directory:

# Change the path from  which the error page templates are loaded. 
# Default value: 'shared/http_status'

HTTPStatus::Base.template_path = 'layouts/http_status'

# Add a method to all exception classes
class HTTPStatus::Base
  def hello_world
    'Hello world!'
  end
end
Clone this wiki locally