<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;Once the plugin is installed, it will register a list of exceptions that you can raise in your controller code. See [[AvailableExceptions]] for the actual list of exceptions and their corresponding status codes.&lt;/p&gt;
&lt;h2&gt;Raising HTTPStatus exceptions&lt;/h2&gt;
&lt;p&gt;You can simply raise an &lt;code&gt;HTTPStatus&lt;/code&gt;-exception as soon as the normal execution of your code should stop.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
class BlogController &amp;lt; ApplicationController&lt;br /&gt;
  def destroy&lt;br /&gt;
    raise HTTPStatus::Forbidden, &amp;#8220;You cannot delete blogs!&amp;#8221; unless user.admin?&lt;/p&gt;
@blog = Blog.find(params[:id])
@blog.destroy
&amp;#8230;
end
&lt;p&gt;end&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Raising exceptions works just as well in a &lt;code&gt;before_filter&lt;/code&gt;:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
class ApplicationController &amp;lt; ActionController::Base&lt;br /&gt;
  before_filter :set_current_user&lt;/p&gt;
def set_current_user
@user = User.find(session[:user_id)
raise HTTPStatus::PaymentRequired, &amp;#8220;Your account is expired&amp;#8221; if @user.is_expired?
end
&lt;p&gt;end&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can provide some additional details to the exception as well by passing an additional argument. This information can be used later on in the exception handler using the &lt;code&gt;details&lt;/code&gt; function.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
begin&lt;br /&gt;
  raise HTTPStatus::Forbidden.new(&amp;#8220;message&amp;#8221;, {:restricted_object =&amp;gt; @blog })&lt;br /&gt;
rescue HTTPStatus::Base =&amp;gt; e&lt;br /&gt;
  logger.error e.details[:restricted_object].inspect&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt;Displaying an error page&lt;/h2&gt;
&lt;p&gt;Once an &lt;code&gt;HTTPStatus&lt;/code&gt; exception is raised, the default exception handler will be called. By default, it will return an empty response with the correct &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; status code. It can return an error page easily by creating a template for that particular exception. For instance, to create an error page for the &lt;code&gt;HTTPStatus::NotFound&lt;/code&gt; (404) exception, you should create a template file with the following name: &lt;code&gt;app/views/shared/http_status/not_found.html.erb&lt;/code&gt;. The current layout will be applied to the template. The template file could look something like this:&lt;/p&gt;
&lt;pre&gt;
&amp;lt;h1&amp;gt; Not found &amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt; The resource you requested could not be found! &amp;lt;/p&amp;gt;
&amp;lt;p style=&quot;color:red&quot;&amp;gt; &amp;lt;%= h(@exception.message) %&amp;gt; &amp;lt;/p&amp;gt;
&amp;lt;hr /&amp;gt;
&amp;lt;p&amp;gt; &amp;lt;small&amp;gt;HTTP status &amp;lt;%= @exception.status_code %&amp;gt;&amp;lt;/small&amp;gt; &amp;lt;/p&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Note that by using the &lt;strong&gt;.html.erb&lt;/strong&gt; extension, this template will only be used for &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; formatted requests; if the same exception is raised with &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; as format, the response will remain empty. In theory, you could create the file &lt;code&gt;app/views/shared/http_status/not_found.xml.builder&lt;/code&gt; to generate an error page for &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; requests. A template file without a format &lt;code&gt;app/views/shared/http_status/not_found.erb&lt;/code&gt; will be used for any format. This is basically the same way Rails handles templates.&lt;/p&gt;
&lt;p&gt;This simple exception handler should be sufficient for most situations. However, it is easy to customize the exception handler to your own liking. See [[Customization]] for details.&lt;/p&gt;</body>
  <created-at type="datetime">2008-09-21T00:32:53-07:00</created-at>
  <id type="integer">58496</id>
  <permalink>basicusage</permalink>
  <repository-id type="integer">54644</repository-id>
  <title>BasicUsage</title>
  <updated-at type="datetime">2008-09-21T01:20:48-07:00</updated-at>
  <user-id type="integer">15870</user-id>
</wiki>
