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
66 changes: 66 additions & 0 deletions src/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,69 @@ describe('Compose', function () {
expect(ctx.get('next')).toEqual(1)
})
})

describe('compose - middleware redirect after error', () => {
it('should allow middleware to redirect after catching an error', async () => {
const middleware: MiddlewareTuple[] = []

const req = new Request('http://localhost/')
const c: Context = new Context(req)

// Middleware that catches errors and redirects
const errorHandlerMiddleware = async (c: Context, next: Next) => {
await next()
if (c.error) {
return c.redirect('/error-page')
}
}

// Handler that throws an error
const handler = () => {
throw new Error('Something went wrong')
}

middleware.push(buildMiddlewareTuple(errorHandlerMiddleware))
middleware.push(buildMiddlewareTuple(handler))

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

const composed = compose(middleware, onError)
const context = await composed(c)

expect(context.res).not.toBeNull()
expect(context.res.status).toBe(302)
expect(context.res.headers.get('Location')).toBe('/error-page')
expect(context.finalized).toBe(true)
})

it('should allow middleware to handle errors without redirect', async () => {
const middleware: MiddlewareTuple[] = []

const req = new Request('http://localhost/')
const c: Context = new Context(req)

// Middleware that just checks for errors but does nothing
const checkMiddleware = async (c: Context, next: Next) => {
await next()
// Check error but don't return anything, let error response pass through
}

// Handler that throws an error
const handler = () => {
throw new Error('Something went wrong')
}

middleware.push(buildMiddlewareTuple(checkMiddleware))
middleware.push(buildMiddlewareTuple(handler))

const onError = (_error: Error, c: Context) => c.text('Error handled', 500)

const composed = compose(middleware, onError)
const context = await composed(c)

expect(context.res).not.toBeNull()
expect(context.res.status).toBe(500)
expect(await context.res.text()).toBe('Error handled')
expect(context.finalized).toBe(true)
})
})
8 changes: 6 additions & 2 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ export const compose = <E extends Env = Env>(
}
}

if (res && (context.finalized === false || isError)) {
context.res = res
if (res) {
// Allow middleware to override error responses even after context is finalized
// This enables middleware to handle errors gracefully (e.g., redirecting)
if (context.finalized === false || isError || (context.error && res !== context.res)) {
context.res = res
}
}
return context
}
Expand Down
Loading