Skip to content

Commit

Permalink
🔧 fix: generic error return 200
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 27, 2024
1 parent 41f5db7 commit 958fde0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.8 - 27 Dec 2024
Bug fix:
- [#966](https://github.com/elysiajs/elysia/issues/966) generic error somehow return 200

# 1.2.7 - 27 Dec 2024
Bug fix:
- macro doesn't work with guard
Expand All @@ -7,7 +11,7 @@ Bug fix:
- [#952](https://github.com/elysiajs/elysia/issues/952) onAfterResponse does not provide mapped response value unless aot is disabled
- `mapResponse.response` is `{}` if no response schema is provided
- Response doesn't reconcile when handler return `Response` is used with `mapResponse`
- `onError` now accept `error` as `number` when `Elysia.error` is thrown (but not return)
- `onError` now accepts `error` as `number` when `Elysia.error` is thrown (but not returned)

# 1.2.6 - 25 Dec 2024
Bug fix:
Expand Down
4 changes: 2 additions & 2 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Elysia, t, error } from '../src'
import { Elysia, t, error, StatusMap } from '../src'
import { req } from '../test/utils'

const app = new Elysia()
.get('/', () => {
throw error("I'm a teapot")
throw new Error("A")
})
.listen(3000)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.2.7",
"version": "1.2.8",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/web-standard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const WebStandardAdapter: ElysiaAdapter = {
unknownError:
`return new Response(` +
`error.message,` +
`{headers:set.headers,status:error.status}` +
`{headers:set.headers,status:error.status??set.status??500}` +
`)`
},
listen() {
Expand Down
30 changes: 30 additions & 0 deletions test/core/handle-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,34 @@ describe('Handle Error', () => {
expect(response.headers.get('content-type')).toStartWith('text/plain')
expect(response.status).toEqual(422)
})

it('handle generic error', async () => {
const res = await new Elysia()
.get('/', () => 'Hi')
// @ts-expect-error private
.handleError(
{
request,
set: {
headers: {}
}
},
// https://youtube.com/shorts/PbIWVPKHfrQ
new Error('a')
)

expect(await res.text()).toBe('a')
expect(res.status).toBe(500)
})

it('handle generic error when thrown in handler', async () => {
const app = new Elysia().get('/', () => {
throw new Error('a')
})

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

expect(await res.text()).toBe('a')
expect(res.status).toBe(500)
})
})

0 comments on commit 958fde0

Please sign in to comment.