Question

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)

Was it helpful?

Solution

Doh. All I need is to set status 400:

error ParamXMissingError do
  status 400
  haml :custom_error_page
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top