[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: add tests for error-handler (#1056)

authored by

James Garbutt and committed by
GitHub
5ab76de9 2273d3bb

+51
+1
package.json
··· 114 114 "@vue/test-utils": "2.4.6", 115 115 "axe-core": "4.11.1", 116 116 "fast-check": "4.5.3", 117 + "h3": "1.15.5", 117 118 "knip": "5.83.0", 118 119 "lint-staged": "16.2.7", 119 120 "oxfmt": "0.27.0",
+3
pnpm-lock.yaml
··· 240 240 fast-check: 241 241 specifier: 4.5.3 242 242 version: 4.5.3 243 + h3: 244 + specifier: 1.15.5 245 + version: 1.15.5 243 246 knip: 244 247 specifier: 5.83.0 245 248 version: 5.83.0(@types/node@24.10.9)(typescript@5.9.3)
+47
test/unit/server/utils/error-handler.spec.ts
··· 1 + import { describe, expect, it } from 'vitest' 2 + import { createError } from 'h3' 3 + import * as v from 'valibot' 4 + import { handleApiError } from '../../../../server/utils/error-handler' 5 + 6 + describe('handleApiError', () => { 7 + const fallback = { message: 'Something went wrong', statusCode: 500 } 8 + 9 + it('re-throws H3 errors as-is', () => { 10 + const h3Err = createError({ statusCode: 404, message: 'Not found' }) 11 + 12 + expect(() => handleApiError(h3Err, fallback)).toThrow(h3Err) 13 + }) 14 + 15 + it('throws a 404 with the first issue message for valibot errors', () => { 16 + const schema = v.object({ name: v.pipe(v.string(), v.minLength(1, 'Name is required')) }) 17 + 18 + let valibotError: unknown 19 + try { 20 + v.parse(schema, { name: '' }) 21 + } catch (e) { 22 + valibotError = e 23 + } 24 + 25 + expect(() => handleApiError(valibotError, fallback)).toThrow( 26 + expect.objectContaining({ statusCode: 404, message: 'Name is required' }), 27 + ) 28 + }) 29 + 30 + it('throws a fallback error with the given statusCode and message', () => { 31 + expect(() => 32 + handleApiError(new Error('unexpected'), { message: 'Bad gateway', statusCode: 502 }), 33 + ).toThrow(expect.objectContaining({ statusCode: 502, message: 'Bad gateway' })) 34 + }) 35 + 36 + it('defaults fallback statusCode to 502 when not provided', () => { 37 + expect(() => handleApiError('some string error', { message: 'Upstream failed' })).toThrow( 38 + expect.objectContaining({ statusCode: 502, message: 'Upstream failed' }), 39 + ) 40 + }) 41 + 42 + it('uses the custom fallback statusCode when provided', () => { 43 + expect(() => handleApiError(null, { message: 'Service unavailable', statusCode: 503 })).toThrow( 44 + expect.objectContaining({ statusCode: 503, message: 'Service unavailable' }), 45 + ) 46 + }) 47 + })