Skip to content

Commit 737f98f

Browse files
committed
Add before_all filter execution for 404 errors in FilterHandler
1 parent 4352774 commit 737f98f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

spec/middleware/filters_spec.cr

+18
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ describe "Kemal::FilterHandler" do
207207
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
208208
client_response.body.should eq("true-true")
209209
end
210+
211+
it "executes before_all filter on 404" do
212+
before_filter = FilterTest.new
213+
before_filter.modified = "false"
214+
215+
filter_middleware = Kemal::FilterHandler.new
216+
filter_middleware._add_route_filter("ALL", "*", :before) { before_filter.modified = "true" }
217+
218+
kemal = Kemal::RouteHandler::INSTANCE
219+
220+
error 404 do
221+
before_filter.modified
222+
end
223+
224+
request = HTTP::Request.new("GET", "/not_found")
225+
client_response = call_request_on_app(request)
226+
client_response.body.should eq("true")
227+
end
210228
end
211229

212230
class FilterTest

src/kemal/filter_handler.cr

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ module Kemal
1313

1414
# The call order of the filters is `before_all -> before_x -> X -> after_x -> after_all`.
1515
def call(context : HTTP::Server::Context)
16-
return call_next(context) unless context.route_found?
16+
if !context.route_found?
17+
if Kemal.config.error_handlers.has_key?(404)
18+
call_block_for_path_type("ALL", context.request.path, :before, context)
19+
end
20+
return call_next(context)
21+
end
22+
1723
call_block_for_path_type("ALL", context.request.path, :before, context)
1824
call_block_for_path_type(context.request.method, context.request.path, :before, context)
1925
if Kemal.config.error_handlers.has_key?(context.response.status_code)

0 commit comments

Comments
 (0)