Skip to content
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"@vue/test-utils": "2.4.6",
"axe-core": "4.11.1",
"fast-check": "4.5.3",
"h3": "1.15.5",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its a transitive dependency but we need it as a direct devDependency for sake of resolving types in tests

"knip": "5.83.0",
"lint-staged": "16.2.7",
"oxfmt": "0.27.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions test/unit/server/utils/error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest'
import { createError } from 'h3'
import * as v from 'valibot'
import { handleApiError } from '../../../../server/utils/error-handler'

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
}
Comment on lines +18 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wanted to use safe parse and construct the v.ValiError yourself but it somehow feels like more work 🫠


expect(() => handleApiError(valibotError, fallback)).toThrow(
expect.objectContaining({ statusCode: 404, message: 'Name is required' }),
)
})

it('throws a fallback error with the given statusCode and message', () => {
expect(() =>
handleApiError(new Error('unexpected'), { message: 'Bad gateway', statusCode: 502 }),
).toThrow(expect.objectContaining({ statusCode: 502, message: 'Bad gateway' }))
})

it('defaults fallback statusCode to 502 when not provided', () => {
expect(() => handleApiError('some string error', { message: 'Upstream failed' })).toThrow(
expect.objectContaining({ statusCode: 502, message: 'Upstream failed' }),
)
})

it('uses the custom fallback statusCode when provided', () => {
expect(() => handleApiError(null, { message: 'Service unavailable', statusCode: 503 })).toThrow(
expect.objectContaining({ statusCode: 503, message: 'Service unavailable' }),
)
})
})
Loading