Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting status code in Sinatra's custom error block

I have a custom error message for 400 status code:

get '/do' do
  raise ParamXMissingError unless params['x']
end

error ParamXMissingError do
  haml :custom_error_page
end

I want ParamXMissingError to be 400, but when I run the above code, and check through Firefox Network tools, it seems Sinatra is actually returning 500, not 400. How do I make it display the custom_error_page.haml and return 400?

Preferably, it'd be nice to have the status code and the page handled from inside the error block, not something that I would sprinkle around when raising ParamXMissingError. For example, this would be repetitive and not a good solution:halt 400, haml(:custom_error_page)

like image 202
garbagecollector Avatar asked Jan 15 '14 23:01

garbagecollector


1 Answers

Doh. All I need is to set status 400:

error ParamXMissingError do
  status 400
  haml :custom_error_page
end
like image 55
garbagecollector Avatar answered Oct 15 '22 04:10

garbagecollector