-
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy patherror-handler.spec.ts
More file actions
55 lines (45 loc) · 1.79 KB
/
error-handler.spec.ts
File metadata and controls
55 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { describe, expect, it } from 'vitest'
import { createError } from 'h3'
import * as v from 'valibot'
import { handleApiError } from '../../../../server/utils/error-handler'
function h3Error(statusCode: number, message: string) {
const error = createError({ statusCode, message })
error.statusCode = statusCode
return error
}
describe('handleApiError', () => {
const fallback = { message: 'Something went wrong', statusCode: 500 }
it('re-throws H3 errors as-is', () => {
const h3Err = createError({ statusCode: 404, message: 'Not found' })
expect(() => handleApiError(h3Err, fallback)).toThrow(h3Err)
})
it('throws a 404 with the first issue message for valibot errors', () => {
const schema = v.object({ name: v.pipe(v.string(), v.minLength(1, 'Name is required')) })
let valibotError: unknown
try {
v.parse(schema, { name: '' })
} catch (e) {
valibotError = e
}
const expected = h3Error(404, 'Name is required')
expect(() => handleApiError(valibotError, fallback)).toThrow(expected)
})
it('throws a fallback error with the given statusCode and message', () => {
const expected = h3Error(502, 'Bad gateway')
expect(() =>
handleApiError(new Error('unexpected'), { message: 'Bad gateway', statusCode: 502 }),
).toThrow(expected)
})
it('defaults fallback statusCode to 502 when not provided', () => {
const expected = h3Error(502, 'Upstream failed')
expect(() => handleApiError('some string error', { message: 'Upstream failed' })).toThrow(
expected,
)
})
it('uses the custom fallback statusCode when provided', () => {
const expected = h3Error(503, 'Service unavailable')
expect(() => handleApiError(null, { message: 'Service unavailable', statusCode: 503 })).toThrow(
expected,
)
})
})