Skip to content

Commit

Permalink
Merge pull request #976 from bogeychan/bogey/974
Browse files Browse the repository at this point in the history
fix: error responses with aot: false
  • Loading branch information
SaltyAom authored Feb 16, 2025
2 parents 7578faa + 72ced01 commit 042a3e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/dynamic-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AnyElysia } from '.'
import {
ElysiaCustomStatusResponse,
ElysiaErrors,
error,
NotFoundError,
ValidationError
} from './error'
Expand Down Expand Up @@ -65,6 +66,7 @@ export const createDynamicHandler = (app: AnyElysia) => {
request,
path,
qi,
error,
redirect
}
) as unknown as Context & {
Expand Down
66 changes: 40 additions & 26 deletions test/lifecycle/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,43 +120,57 @@ describe('error', () => {
expect(response.status).toBe(500)
})

it('return correct number status on error function', async () => {
const app = new Elysia().get('/', ({ error }) =>
error(418, 'I am a teapot')
)
it.each([true, false])(
'return correct number status on error function with aot: %p',
async (aot) => {
const app = new Elysia({ aot }).get('/', ({ error }) =>
error(418, 'I am a teapot')
)

const response = await app.handle(req('/'))
const response = await app.handle(req('/'))

expect(response.status).toBe(418)
})
expect(response.status).toBe(418)
}
)

it('return correct named status on error function', async () => {
const app = new Elysia().get('/', ({ error }) =>
error("I'm a teapot", 'I am a teapot')
)
it.each([true, false])(
'return correct named status on error function with aot: %p',
async (aot) => {
const app = new Elysia({ aot }).get('/', ({ error }) =>
error("I'm a teapot", 'I am a teapot')
)

const response = await app.handle(req('/'))
const response = await app.handle(req('/'))

expect(response.status).toBe(418)
})
expect(response.status).toBe(418)
}
)

it('return correct number status without value on error function', async () => {
const app = new Elysia().get('/', ({ error }) => error(418))
it.each([true, false])(
'return correct number status without value on error function with aot: %p',
async (aot) => {
const app = new Elysia({ aot }).get('/', ({ error }) => error(418))

const response = await app.handle(req('/'))
const response = await app.handle(req('/'))

expect(response.status).toBe(418)
expect(await response.text()).toBe("I'm a teapot")
})
expect(response.status).toBe(418)
expect(await response.text()).toBe("I'm a teapot")
}
)

it('return correct named status without value on error function', async () => {
const app = new Elysia().get('/', ({ error }) => error("I'm a teapot"))
it.each([true, false])(
'return correct named status without value on error function with aot: %p',
async (aot) => {
const app = new Elysia({ aot }).get('/', ({ error }) =>
error("I'm a teapot")
)

const response = await app.handle(req('/'))
const response = await app.handle(req('/'))

expect(response.status).toBe(418)
expect(await response.text()).toBe("I'm a teapot")
})
expect(response.status).toBe(418)
expect(await response.text()).toBe("I'm a teapot")
}
)

it('handle error in order', async () => {
let order = <string[]>[]
Expand Down

0 comments on commit 042a3e8

Please sign in to comment.