Skip to content

Commit 453a548

Browse files
benhidHugoRCD
andauthored
fix(evlog): respect status/statusCode on Error instances (#218)
Co-authored-by: Hugo Richard <hugo.richard@vercel.com>
1 parent 79f811d commit 453a548

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"evlog": patch
3+
---
4+
5+
fix(parseError): respect `.status` / `.statusCode` on Error instances instead of hardcoding 500
6+
7+
Frameworks like NestJS attach HTTP status directly on Error subclasses (e.g. `BadRequestException` has `.status = 400`). Previously, `parseError()` ignored these properties and always returned 500 for any `Error` instance without a `data` property. Now uses `extractErrorStatus()` to extract the correct status.

packages/evlog/src/runtime/utils/parseError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FetchError } from 'ofetch'
22
import type { ParsedError } from '../../types'
3+
import { extractErrorStatus } from '../../shared/errors'
34

45
export type { ParsedError }
56

@@ -25,7 +26,7 @@ export function parseError(error: unknown): ParsedError {
2526
if (error instanceof Error) {
2627
return {
2728
message: error.message,
28-
status: 500,
29+
status: extractErrorStatus(error),
2930
raw: error,
3031
}
3132
}

packages/evlog/test/error.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,47 @@ describe('parseError', () => {
317317
expect(parsed.status).toBe(500)
318318
})
319319
})
320+
321+
describe('Error instances with status properties', () => {
322+
it('respects .status on Error', () => {
323+
const error = Object.assign(new Error('Bad Request'), { status: 400 })
324+
325+
const parsed = parseError(error)
326+
327+
expect(parsed.status).toBe(400)
328+
expect(parsed.message).toBe('Bad Request')
329+
})
330+
331+
it('respects .statusCode on Error', () => {
332+
const error = Object.assign(new Error('Not Found'), { statusCode: 404 })
333+
334+
const parsed = parseError(error)
335+
336+
expect(parsed.status).toBe(404)
337+
})
338+
339+
it('prefers .status over .statusCode', () => {
340+
const error = Object.assign(new Error('Conflict'), { status: 409, statusCode: 500 })
341+
342+
const parsed = parseError(error)
343+
344+
expect(parsed.status).toBe(409)
345+
})
346+
347+
it('defaults to 500 for plain Error without status', () => {
348+
const error = new Error('Something broke')
349+
350+
const parsed = parseError(error)
351+
352+
expect(parsed.status).toBe(500)
353+
})
354+
355+
it('defaults to 500 when status is not a valid number', () => {
356+
const error = Object.assign(new Error('Weird'), { status: 'bad' })
357+
358+
const parsed = parseError(error)
359+
360+
expect(parsed.status).toBe(500)
361+
})
362+
})
320363
})

0 commit comments

Comments
 (0)