|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | 2 | import { createError } from 'h3' |
| 3 | +import { FetchError } from 'ofetch' |
3 | 4 | import * as v from 'valibot' |
4 | 5 | import { handleApiError } from '../../../../server/utils/error-handler' |
5 | 6 |
|
@@ -44,4 +45,34 @@ describe('handleApiError', () => { |
44 | 45 | expect.objectContaining({ statusCode: 503, message: 'Service unavailable' }), |
45 | 46 | ) |
46 | 47 | }) |
| 48 | + |
| 49 | + describe('FetchError handling', () => { |
| 50 | + it('propagates the upstream statusCode from a FetchError', () => { |
| 51 | + const fetchErr = new FetchError('Not Found') |
| 52 | + fetchErr.statusCode = 404 |
| 53 | + fetchErr.statusMessage = 'Not Found' |
| 54 | + |
| 55 | + expect(() => handleApiError(fetchErr, fallback)).toThrow( |
| 56 | + expect.objectContaining({ statusCode: 404, message: 'Not Found' }), |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + it('propagates a 503 statusCode from a FetchError', () => { |
| 61 | + const fetchErr = new FetchError('Service Unavailable') |
| 62 | + fetchErr.statusCode = 503 |
| 63 | + fetchErr.statusMessage = 'Service Unavailable' |
| 64 | + |
| 65 | + expect(() => handleApiError(fetchErr, fallback)).toThrow( |
| 66 | + expect.objectContaining({ statusCode: 503 }), |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('falls through to the generic fallback when FetchError has no statusCode', () => { |
| 71 | + const fetchErr = new FetchError('Network error') |
| 72 | + |
| 73 | + expect(() => handleApiError(fetchErr, { message: 'Bad gateway', statusCode: 502 })).toThrow( |
| 74 | + expect.objectContaining({ statusCode: 502, message: 'Bad gateway' }), |
| 75 | + ) |
| 76 | + }) |
| 77 | + }) |
47 | 78 | }) |
0 commit comments