title: How to raise a custom error code in sinatra?
body: <p>I did the following in my sinatra app:</p>
  
  <pre><code>disable :show_exceptions
  disable :raise_errors
  
  error do
    haml :error, :locals =&gt; {:error_message =&gt; request.env['sinatra.error'].to_s}
  end
  
  get '/error' do
    raise "ERROR!!"
  end
  </code></pre>
  
  <p>If I visit <code>/error</code> I get a <code>500 - Internal Server Error</code> response code, which is god and wanted. But how do I change the code to, eg, 404 or 501?</p>
  
  <p><strong>The answer:</strong></p>
  
  <pre><code>disable :show_exceptions
  disable :raise_errors
  
  get '/error' do
    halt(404,haml(:error, :locals =&gt; {:error_message =&gt; request.env['sinatra.error'].to_s}))
  end
  </code></pre>