feat: throw error if context is not finalized (#301)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: throw error if context is not finalized * remove duplicate
authored by Yusuke Wada · committed by GitHub · parent 7c244c3 · Browse files · 3 files·+83 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+83 added−0 removed
- tests:+78 added−0 removed
- other:+5 added−0 removed
Provenance
- Commit
0de127f- Tree
6f068bc- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7c244c3
3 files changed+83 added−0 removed
modified:src/compose.test.ts+44 added
139 unmodified lines140 const context = await composed(c)141 expect(context.res).not.toBeNull()142 expect(context.res.status).toBe(404)143 expect(await context.res.text()).toBe('onNotFound')144+ expect(context.finalized).toBe(true)145 })146 })147 148 describe('compose with Context - 401 not authorized', () => {20 unmodified lines169 const context = await composed(c)170 expect(context.res).not.toBeNull()171 expect(context.res.status).toBe(401)172 expect(await context.res.text()).toBe('Not authorized')173+ expect(context.finalized).toBe(true)174 })175 })176 177 describe('compose with Context - next() below', () => {21 unmodified lines199 const context = await composed(c)200 expect(context.res).not.toBeNull()201 expect(context.res.status).toBe(200)202 expect(await context.res.text()).toBe('foo')203+ expect(context.finalized).toBe(true)204 })205 })206 207 describe('compose with Context - 500 error', () => {21 unmodified lines229 const context = await composed(c)230 expect(context.res).not.toBeNull()231 expect(context.res.status).toBe(500)232 expect(await context.res.text()).toBe('onError')233+ expect(context.finalized).toBe(true)234 })235 236 it('Error on middleware', async () => {237 const handler = (c: Context) => {11 unmodified lines249 const context = await composed(c)250 expect(c.res).not.toBeNull()251 expect(c.res.status).toBe(500)252 expect(await context.res.text()).toBe('onError')253+ expect(context.finalized).toBe(true)254 })255 256 it('Run all the middlewares', async () => {257 const ctx: C = { req: {}, res: {} }16 unmodified lines274 await composed(ctx)275 expect(stack).toEqual([0, 1, 2])276 })277 })278+describe('compose with Context - not finalized', () => {279+ const req = new Request('http://localhost/')280+ const c: Context = new Context(req)281 282+ const onError = (_error: Error, c: Context) => {283+ return c.text('onError', 500)284+ }285+ const onNotFound = (c: Context) => {286+ return c.text('onNotFound', 404)287+ }288+289+ it('Should not be finalized - lack `next()`', async () => {290+ const middleware: Function[] = []291+ const mHandler = async (_c: Context, next: Function) => {292+ await next()293+ }294+ const mHandler2 = async () => {}295+296+ middleware.push(mHandler)297+ middleware.push(mHandler2)298+ const composed = compose<Context>(middleware, onError, onNotFound)299+ const context = await composed(c)300+ expect(context.finalized).toBe(false)301+ })302+303+ it('Should not be finalized - lack `return Response`', async () => {304+ const middleware2: Function[] = []305+ const mHandler3 = async (_c: Context, next: Function) => {306+ await next()307+ }308+ const handler = async () => {}309+ middleware2.push(mHandler3)310+ middleware2.push(handler)311+312+ const composed = compose<Context>(middleware2, onError, onNotFound)313+ const context = await composed(c)314+ expect(context.finalized).toBe(false)315+ })316+})317+318 describe('Compose', function () {319 function isPromise(x: any) {320 return x && typeof x.then === 'function'321 }355 unmodified linesmodified:src/hono.test.ts+34 added
838 unmodified lines839 expect(res.headers.get('foo2')).toBe('bar2')840 })841 })842 })843+844+describe('Context is not finalized', () => {845+ it('should throw error - lack `await next()`', async () => {846+ const app = new Hono()847+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment848+ // @ts-ignore849+ app.use('*', () => {})850+ app.get('/foo', (c) => {851+ return c.text('foo')852+ })853+ app.onError((err, c) => {854+ return c.text(err.message, 500)855+ })856+ const res = await app.request('http://localhost/foo')857+ expect(res.status).toBe(500)858+ expect(await res.text()).toMatch(/^Context is not finalized/)859+ })860+861+ it('should throw error - lack `returning Response`', async () => {862+ const app = new Hono()863+ app.use('*', async (_c, next) => {864+ await next()865+ })866+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment867+ // @ts-ignore868+ app.get('/foo', () => {})869+ app.onError((err, c) => {870+ return c.text(err.message, 500)871+ })872+ const res = await app.request('http://localhost/foo')873+ expect(res.status).toBe(500)874+ expect(await res.text()).toMatch(/^Context is not finalized/)875+ })876+})modified:src/hono.ts+5 added
174 unmodified lines175 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)176 let context: Context177 try {178 context = await composed(c)179+ if (!context.finalized) {180+ throw new Error(181+ 'Context is not finalized. You may forget returning Response object or `await next()`'182+ )183+ }184 } catch (err) {185 if (err instanceof Error) {186 return this.errorHandler(err, c)187 }25 unmodified lines