How to raise a custom error code in sinatra?
Something like raise 404
raises an error just like raise ZeroDivisionError
would, which causes your app to throw a 500 Internal Server Error. The simplest way to return a specific error is to use status
get '/raise404' do status 404end
You can also add a custom response body with body
get '/raise403' do status 403 body 'This is a 403 error'end
I use this in block
if 'condition' then do somethingelse halt 500 , "error message"end #only without errorerb :my_template
In case of error my log is like this
HTTP/1.1" 500 13 0.1000
Instead of raise "ERROR!!"
, try just doing error 404
or error 501
with optional status message after the status code.
Update: If you define your error handler as error 400..501 do...
for example, you can use error 501 "ERROR!!"
in your "/error"
route. This will also put your "ERROR!!" message in env['sinatra.error'].message
.