Skip to content

Commit 0335e86

Browse files
authored
Add before_all filter execution for 404 errors in FilterHandler (#706)
1 parent e3de2ad commit 0335e86

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

spec/middleware/filters_spec.cr

+16
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ 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+
error 404 do
219+
before_filter.modified
220+
end
221+
222+
request = HTTP::Request.new("GET", "/not_found")
223+
client_response = call_request_on_app(request)
224+
client_response.body.should eq("true")
225+
end
210226
end
211227

212228
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)