简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM