Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,39 @@ describe('compose with Context - 500 error', () => {
expect(context.finalized).toBe(true)
})

it('Should allow middleware to redirect after error', async () => {
const redirectMid = async (c: Context, next: Next) => {
await next()
if (c.error) {
return c.redirect('/foo')
}
}

const handler = (c: Context) => {
const name = c.req.query('name')
if (name === 'error') {
throw new Error()
}
return c.text(`Hello ${name}!`)
}

const testMiddleware: MiddlewareTuple[] = []
testMiddleware.push(buildMiddlewareTuple(redirectMid))
testMiddleware.push(buildMiddlewareTuple(handler))

const onError = (_error: Error, c: Context) => c.text('Internal Server Error', 500)
const onNotFound = (c: Context) => c.text('NotFound', 404)

const req = new Request('http://localhost/hello?name=error')
const c: Context = new Context(req)
const composed = compose(testMiddleware, onError, onNotFound)
const context = await composed(c)
expect(context.res).not.toBeNull()
expect(context.res.status).toBe(302)
expect(context.res.headers.get('Location')).toBe('/foo')
expect(context.finalized).toBe(true)
})

it('Run all the middlewares', async () => {
const stack: number[] = []
const middlewares = [
Expand Down
2 changes: 1 addition & 1 deletion src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const compose = <E extends Env = Env>(
}
}

if (res && (context.finalized === false || isError)) {
if (res && (context.finalized === false || isError || context.error)) {
context.res = res
}
return context
Expand Down
Loading