Ruby and Sinatra
I started writing a simple Sinatra app today and I am trying to understand the error reporting but for some reason I can't get it to work correctly.
I know here, http://railsapi.com/doc/sinatra-v1.0/, it talks about working with error reporting/handling but when I run their examples I can't get it to work.
require 'sinatra'
error 400..510 do
'Boom'
end
get '/say/*' do
params[:splat]
end
When I run the app on my computer I get a 404 error code, but the 'Boom'
text does开发者_JS百科 not display in the browser, just the browser 404 page. I am sure I am doing something wrong, but just can't figure it out.
I will wager its your browser. On my MacBook Pro:
Chrome "helpfully" displays a "Oops! This link appears to be broken." page.
Safari displays the expected Boom
text.
Firefox displays the expected Boom
text.
It seems that Sinatra throws Sinatra::NotFound exception (404) to a specific handler. Simply modify the code as follows,
require 'sinatra'
not_found do
'Boom in NOT_FOUND.'
end
error 400..510 do
'Boom'
end
get '/say/*' do
params[:splat]
end
It works well in Chrome and Firefox on Mac OSX.
精彩评论