feat: add error formatting tests and interfaces for consistent error handling

This commit is contained in:
2025-12-29 22:21:12 +00:00
parent c2997c915a
commit d0be4da56c
6 changed files with 92 additions and 9 deletions

View File

@@ -0,0 +1,67 @@
import { describe, it, expect } from 'vitest'
import { formatError } from './format-error'
import { DBALError, DBALErrorCode } from '@/dbal/development/src/core/foundation/errors'
describe('formatError', () => {
it.each([
{
error: new DBALError(DBALErrorCode.NOT_FOUND, 'Resource not found'),
expected: {
message: 'Resource not found',
code: 404,
details: undefined,
},
description: 'DBALError without details',
},
{
error: new DBALError(DBALErrorCode.VALIDATION_ERROR, 'Invalid input', {
fields: [{ field: 'email', error: 'Invalid format' }],
}),
expected: {
message: 'Invalid input',
code: 422,
details: { fields: [{ field: 'email', error: 'Invalid format' }] },
},
description: 'DBALError with details',
},
{
error: new Error('Standard error'),
expected: {
message: 'Standard error',
stack: expect.any(String),
},
description: 'Standard Error',
},
{
error: 'String error',
expected: {
message: 'String error',
},
description: 'String error',
},
{
error: { message: 'Custom error' },
expected: {
message: 'Custom error',
},
description: 'Error-like object',
},
{
error: null,
expected: {
message: 'An unknown error occurred',
},
description: 'null',
},
{
error: undefined,
expected: {
message: 'An unknown error occurred',
},
description: 'undefined',
},
])('should format $description correctly', ({ error, expected }) => {
const result = formatError(error)
expect(result).toMatchObject(expected)
})
})

View File

@@ -1,12 +1,6 @@
import { DBALError, DBALErrorCode } from '@/dbal/development/src/core/foundation/errors'
import { DBALError } from '@/dbal/core/foundation/errors'
import { getErrorMessage, isError } from '@/lib/types/guards'
export interface FormattedError {
message: string
code?: DBALErrorCode | number
stack?: string
details?: Record<string, unknown>
}
import type { FormattedError } from './interfaces/formatted-error'
/**
* Format any error into a consistent structure

View File

@@ -1,6 +1,8 @@
export { formatError, toUserMessage } from './format-error'
export { formatError } from './format-error'
export type { FormattedError } from './format-error'
export { toUserMessage } from './to-user-message'
export { handleAPIError } from './handle-api-error'
export type { APIErrorResponse } from './handle-api-error'

View File

@@ -0,0 +1,6 @@
export interface APIErrorResponse {
error: string
code?: number
details?: Record<string, unknown>
timestamp?: string
}

View File

@@ -0,0 +1,8 @@
import type { DBALErrorCode } from '@/dbal/development/src/core/foundation/errors'
export interface FormattedError {
message: string
code?: DBALErrorCode | number
stack?: string
details?: Record<string, unknown>
}

View File

@@ -0,0 +1,6 @@
export interface LogContext {
component?: string
userId?: string
action?: string
[key: string]: unknown
}