|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { APIError } from '../errors/APIError.js' |
| 4 | +import { ValidationError } from '../errors/ValidationError.js' |
| 5 | +import { formatErrors } from './formatErrors.js' |
| 6 | + |
| 7 | +describe('formatErrors', () => { |
| 8 | + it('should format a Payload ValidationError', () => { |
| 9 | + const err = new ValidationError({ |
| 10 | + errors: [{ message: 'Field is required', path: 'title' }], |
| 11 | + }) |
| 12 | + |
| 13 | + const result = formatErrors(err) |
| 14 | + |
| 15 | + expect(result.errors).toHaveLength(1) |
| 16 | + expect(result.errors[0]).toMatchObject({ |
| 17 | + name: 'ValidationError', |
| 18 | + message: expect.stringContaining('title'), |
| 19 | + data: { errors: [{ message: 'Field is required', path: 'title' }] }, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should format a Payload APIError', () => { |
| 24 | + const err = new APIError('Something went wrong', 400, { detail: 'bad input' }, true) |
| 25 | + |
| 26 | + const result = formatErrors(err) |
| 27 | + |
| 28 | + expect(result.errors).toHaveLength(1) |
| 29 | + expect(result.errors[0]).toMatchObject({ |
| 30 | + name: 'APIError', |
| 31 | + message: 'Something went wrong', |
| 32 | + data: { detail: 'bad input' }, |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should format a Mongoose-style ValidationError', () => { |
| 37 | + const mongooseError = { |
| 38 | + name: 'ValidationError', |
| 39 | + errors: { |
| 40 | + email: { path: 'email', message: 'is invalid' }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + const result = formatErrors(mongooseError as any) |
| 45 | + |
| 46 | + expect(result.errors).toHaveLength(1) |
| 47 | + expect(result.errors[0]).toMatchObject({ field: 'email', message: 'is invalid' }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should format an array message error', () => { |
| 51 | + const err = { message: [{ message: 'item one' }, { message: 'item two' }] } |
| 52 | + |
| 53 | + const result = formatErrors(err as any) |
| 54 | + |
| 55 | + expect(result.errors).toHaveLength(2) |
| 56 | + expect(result.errors[0]).toMatchObject({ message: 'item one' }) |
| 57 | + expect(result.errors[1]).toMatchObject({ message: 'item two' }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should format a named non-Payload error', () => { |
| 61 | + const err = new Error('Unexpected failure') |
| 62 | + |
| 63 | + const result = formatErrors(err as any) |
| 64 | + |
| 65 | + expect(result.errors).toHaveLength(1) |
| 66 | + expect(result.errors[0]!.message).toBe('Unexpected failure') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should format a Payload APIError with no data', () => { |
| 70 | + const err = new APIError('Server error') |
| 71 | + |
| 72 | + const result = formatErrors(err) |
| 73 | + |
| 74 | + expect(result.errors).toHaveLength(1) |
| 75 | + expect(result.errors[0]).toStrictEqual({ message: 'Server error' }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should return unknown error for null/undefined input', () => { |
| 79 | + expect(formatErrors(null as any).errors[0]!.message).toBe('An unknown error occurred.') |
| 80 | + expect(formatErrors(undefined as any).errors[0]!.message).toBe('An unknown error occurred.') |
| 81 | + }) |
| 82 | +}) |
0 commit comments