Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Copy Markdown
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.

55 changes: 55 additions & 0 deletions test/unit/server/utils/error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Comment on lines +18 to +23
Copy link
Copy Markdown
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 🫠


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,
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})
})
Loading