Sinatra Basics : Customizing Status Code and Error Message


To learn how to use halt method.


Step 1

require 'sinatra'

get '/' do
  halt 400, 'Validation failed'

You can use the status method, but it does not allow providing a custom error message.

Step 2

Run the application.

$shotgun status.rb

Step 3

Browse to http://localhost:9393.

Step 4

You see status code 400 in the server log and 'Validation failed' in the browser.

Step 5

Change the status code to 500 and refresh the browser. You will now see the status code is 500.


In this article, you learned how to use halt method to customize status code and error message in a Sinatra application.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.