diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index cd790cae..72bb44ac 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1255,13 +1255,13 @@ module Sinatra args = compile! "ERROR", //, block codes = codes.map { |c| Array(c) }.flatten codes << Exception if codes.empty? + codes << Sinatra::NotFound if codes.include?(404) codes.each { |c| (@errors[c] ||= []) << args } end # Sugar for `error(404) { ... }` def not_found(&block) error(404, &block) - error(Sinatra::NotFound, &block) end # Define a named template. The block must return the template source. diff --git a/test/routing_test.rb b/test/routing_test.rb index 556d35b4..57567312 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -171,6 +171,21 @@ class RoutingTest < Minitest::Test assert_equal 404, status 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 mock_app do get '/' do