kemalcr / kemal

Fast, Effective, Simple Web Framework

Home Page:https://kemalcr.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

before_all handler isn't called for error routes

syeopite opened this issue · comments

Description

It seems like the before_all handler isn't called for the error routes.

Steps to Reproduce

  1. Take a simple project with a before_all handler:

    require "kemal"
    
    before_all &.set "message", "hello"
    get "/", &.get("message")
    error 404, &.get("message")
    
    Kemal.run
  2. Query a route that doesn't exist such as 0.0.0.0:3000/route

  3. Notice that the before_all handler isn't called

Expected behavior:

curl "0.0.0.0:3000" # hello
curl "0.0.0.0:3000/path" # hello

Actual behavior: [What actually happens]

curl "0.0.0.0:3000" # hello
curl "0.0.0.0:3000/path" # => internal server error

Reproduces how often: [What percentage of the time does it reproduce?]

100% of the time

Versions

Crystal 1.9.2 (2023-08-24)

LLVM: 16.0.6
Default target: x86_64-pc-linux-gnu

Looks like a bug to. me, thanks a lot for reporting @syeopite I'll put this as priority in my pipeline 👍

My initial findings show that Kemal::Config::INSTANCE.handlers is not working as expected. Instead of respecting Kemal::Config::FILTER_HANDLERS it just errors out flat. Changing the order of the filters didn't work either. Need to investigate if it's a special behavior to 404

My initial findings show that Kemal::Config::INSTANCE.handlers is not working as expected. Instead of respecting Kemal::Config::FILTER_HANDLERS it just errors out flat. Changing the order of the filters didn't work either. Need to investigate if it's a special behavior to 404

Might be the exception handler short-circuiting the response and ignoring filter handlers

It seems, like the filter handler does not get the blocks if a route is not found?

return call_next(context) unless context.route_found?

Commenting that line seems to make the original example work, but from a quick search I could not find the reasoning for it to be there in the first place. Happy to try and put in a PR to fix if you give some pointers on why it's there.