Skip to content
Myr

fix(combine): return a Response from a short-circuiting middleware in some() (#5391)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(combine): return a Response from a short-circuiting middleware in some() A middleware that short-circuits returns a `Response` without calling `next()`, which does not assign `c.res`. `some()` matched the result against `true` and `false`, then dropped it, so the context was never finalized and hono-base threw "Context is not finalized", surfacing as a 500. `every()` has handled this since #3441, whose description is the same report for the sibling: validators and `cors()` on OPTIONS return a Response rather than calling `next()`. `some()` was not part of that change. Keeps the Response and returns it. A `true` that reaches the same branch because the context is already finalized is excluded, so a Condition is still never used as a response. * docs(combine): remove redundant comments ---------

authored by Harshit Prakash · committed by GitHub · parent e5bb206 · Browse files · 2 files+21 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+21 added−0 removed

  • tests:+17 added−0 removed
  • other:+4 added−0 removed

2 files+21 added−0 removed

modified:src/middleware/combine/index.test.ts+17 added
131 unmodified lines
132133 expect(middleware2).not.toBeCalled()134 expect(await res.text()).toBe('oops')135 })136137 it('Should return a Response from a middleware that short-circuits', async () => {138 const shortCircuit: MiddlewareHandler = async (c) => c.json({ error: 'bad request' }, 400)139 const next = vi.fn(async (_c, n) => {140 await n()141 })142143 const app = new Hono()144 app.use('/', some(shortCircuit, next))145 app.get('/', (c) => c.text('handler'))146147 const res = await app.request('http://localhost/')148149 expect(res.status).toBe(400)150 expect(await res.json()).toEqual({ error: 'bad request' })151 expect(next).not.toBeCalled()152 })153})154155describe('every', () => {156 let app: Hono
199 unmodified lines
modified:src/middleware/combine/index.ts+4 added