mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
feat: add error formatting tests and interfaces for consistent error handling
This commit is contained in:
67
frontends/nextjs/src/lib/errors/format-error.test.ts
Normal file
67
frontends/nextjs/src/lib/errors/format-error.test.ts
Normal 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)
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface APIErrorResponse {
|
||||
error: string
|
||||
code?: number
|
||||
details?: Record<string, unknown>
|
||||
timestamp?: string
|
||||
}
|
||||
@@ -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>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface LogContext {
|
||||
component?: string
|
||||
userId?: string
|
||||
action?: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
Reference in New Issue
Block a user