@spfn/core
Version:
SPFN Framework Core - File-based routing, transactions, repository pattern
87 lines (85 loc) • 2.61 kB
TypeScript
/**
* Database Error Classes
*
* Type-safe error handling with custom error class hierarchy
* Mapped to HTTP status codes for API responses
*/
/**
* Base Database Error
*
* Base class for all database-related errors
*/
declare class DatabaseError<TDetails extends Record<string, unknown> = Record<string, unknown>> extends Error {
readonly statusCode: number;
readonly details?: TDetails;
readonly timestamp: Date;
constructor(message: string, statusCode?: number, details?: TDetails);
/**
* Serialize error for API response
*/
toJSON(): {
name: string;
message: string;
statusCode: number;
details: TDetails | undefined;
timestamp: string;
};
}
/**
* Connection Error (503 Service Unavailable)
*
* Database connection failure, connection pool exhaustion, etc.
*/
declare class ConnectionError extends DatabaseError {
constructor(message: string, details?: Record<string, any>);
}
/**
* Query Error (500 Internal Server Error)
*
* SQL query execution failure, syntax errors, etc.
*/
declare class QueryError extends DatabaseError {
constructor(message: string, statusCode?: number, details?: Record<string, any>);
}
/**
* Entity Not Found Error (404 Not Found)
*
* Database entity does not exist
*/
declare class EntityNotFoundError extends QueryError {
constructor(resource: string, id: string | number);
}
/**
* Constraint Violation Error (400 Bad Request)
*
* Database constraint violation (NOT NULL, CHECK, FOREIGN KEY, etc.)
* This is different from HTTP ValidationError which validates request input
*/
declare class ConstraintViolationError extends QueryError {
constructor(message: string, details?: Record<string, any>);
}
/**
* Transaction Error (500 Internal Server Error)
*
* Transaction start/commit/rollback failure
*/
declare class TransactionError extends DatabaseError {
constructor(message: string, statusCode?: number, details?: Record<string, any>);
}
/**
* Deadlock Error (409 Conflict)
*
* Database deadlock detected
*/
declare class DeadlockError extends TransactionError {
constructor(message: string, details?: Record<string, any>);
}
/**
* Duplicate Entry Error (409 Conflict)
*
* Unique constraint violation (e.g., duplicate email)
*/
declare class DuplicateEntryError extends QueryError {
constructor(field: string, value: string | number);
}
export { ConnectionError as C, DatabaseError as D, EntityNotFoundError as E, QueryError as Q, TransactionError as T, ConstraintViolationError as a, DeadlockError as b, DuplicateEntryError as c };