1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Merge pull request #896 from jeremyevans/not-found-404

Unify not_found and error 404 behavior (Fixes #814)
This commit is contained in:
Zachary Scott 2016-01-31 20:01:58 +09:00
commit 78dc9d8c87
2 changed files with 16 additions and 1 deletions

View file

@ -1255,13 +1255,13 @@ module Sinatra
args = compile! "ERROR", //, block args = compile! "ERROR", //, block
codes = codes.map { |c| Array(c) }.flatten codes = codes.map { |c| Array(c) }.flatten
codes << Exception if codes.empty? codes << Exception if codes.empty?
codes << Sinatra::NotFound if codes.include?(404)
codes.each { |c| (@errors[c] ||= []) << args } codes.each { |c| (@errors[c] ||= []) << args }
end end
# Sugar for `error(404) { ... }` # Sugar for `error(404) { ... }`
def not_found(&block) def not_found(&block)
error(404, &block) error(404, &block)
error(Sinatra::NotFound, &block)
end end
# Define a named template. The block must return the template source. # Define a named template. The block must return the template source.

View file

@ -171,6 +171,21 @@ class RoutingTest < Minitest::Test
assert_equal 404, status assert_equal 404, status
end end
it "uses 404 error handler for not matching route" do
mock_app {
not_found do
"nf"
end
error 404 do
"e"
end
}
get "/"
assert_equal "e", body
assert_equal 404, status
end
it 'matches empty PATH_INFO to "/" if no route is defined for ""' do it 'matches empty PATH_INFO to "/" if no route is defined for ""' do
mock_app do mock_app do
get '/' do get '/' do