2026-06-01 21:44:29 +07:00
|
|
|
// Custom error classes for all services
|
|
|
|
|
|
|
|
|
|
export class AppError extends Error {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
2026-06-02 21:06:42 +07:00
|
|
|
public code: string,
|
|
|
|
|
public statusCode: number = 500,
|
2026-06-01 21:44:29 +07:00
|
|
|
public details?: Record<string, unknown>,
|
|
|
|
|
) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "AppError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ValidationError extends AppError {
|
|
|
|
|
constructor(message: string, details?: Record<string, unknown>) {
|
2026-06-02 21:06:42 +07:00
|
|
|
super(message, "VALIDATION_ERROR", 400, details);
|
2026-06-01 21:44:29 +07:00
|
|
|
this.name = "ValidationError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class NotFoundError extends AppError {
|
|
|
|
|
constructor(resource: string, id?: string) {
|
2026-06-09 10:16:04 +07:00
|
|
|
super(`${resource} not found${id ? `: ${id}` : ""}`, "NOT_FOUND", 404);
|
2026-06-01 21:44:29 +07:00
|
|
|
this.name = "NotFoundError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class UnauthorizedError extends AppError {
|
|
|
|
|
constructor(message = "Unauthorized") {
|
2026-06-02 21:06:42 +07:00
|
|
|
super(message, "UNAUTHORIZED", 401);
|
2026-06-01 21:44:29 +07:00
|
|
|
this.name = "UnauthorizedError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DatabaseError extends AppError {
|
|
|
|
|
constructor(message: string, details?: Record<string, unknown>) {
|
2026-06-02 21:06:42 +07:00
|
|
|
super(message, "DATABASE_ERROR", 500, details);
|
2026-06-01 21:44:29 +07:00
|
|
|
this.name = "DatabaseError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ConfigError extends AppError {
|
|
|
|
|
constructor(message: string) {
|
2026-06-02 21:06:42 +07:00
|
|
|
super(message, "CONFIG_ERROR", 500);
|
2026-06-01 21:44:29 +07:00
|
|
|
this.name = "ConfigError";
|
|
|
|
|
}
|
|
|
|
|
}
|