Skip to content
Myr

fix(combine): halt middleware evaluation if error in `next()` (#3905)

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

* test(combine): add test for error in next function Co-authored-by: satoshun00 <satou-s@aidemy.co.jp> * fix(combine): halt middleware evaluation if error in `next()` * refactor(combine): simplify next function handling * fix(combine): do not call skipped middleware even if next function throws an error with returning truthy value middleware * test(combine): refactor test code * test(combine): Modified tests to cover modified cases ---------

authored by Taku Amano · committed by GitHub · parent 3a7259b · Browse files · 2 files+53 added−3 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: satoshun00 <satou-s@aidemy.co.jp>

Diff stats

2 files+53 added−3 removed

  • tests:+42 added−0 removed
  • other:+11 added−3 removed

2 files+53 added−3 removed

modified:src/middleware/combine/index.test.ts+42 added
89 unmodified lines
90 const res = await app.request('http://localhost/')9192 expect(await res.text()).toBe('oops')93 })9495 it('Should not call skipped middleware even if an error is thrown', async () => {96 const middleware1: MiddlewareHandler = async (c, next) => {97 await next()98 }99 const middleware2 = vi.fn(() => true)100101 app.use(102 '/',103 every(some(middleware1, middleware2), () => {104 throw new Error('Error')105 })106 )107 app.get('/', (c) => c.text('OK'))108 app.onError((_, c) => {109 return c.text('oops')110 })111 const res = await app.request('http://localhost/')112113 expect(middleware2).not.toBeCalled()114 expect(await res.text()).toBe('oops')115 })116117 it('Should not call skipped middleware even if an error is thrown with returning truthy value middleware', async () => {118 const middleware1 = () => true119 const middleware2 = vi.fn(() => true)120121 app.use(122 '/',123 every(some(middleware1, middleware2), () => {124 throw new Error('Error')125 })126 )127 app.get('/', (c) => c.text('OK'))128 app.onError((_, c) => {129 return c.text('oops')130 })131 const res = await app.request('http://localhost/')132133 expect(middleware2).not.toBeCalled()134 expect(await res.text()).toBe('oops')135 })136})137138describe('every', () => {139 let app: Hono
199 unmodified lines
modified:src/middleware/combine/index.ts+11 added-3 removed