50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
export class HttpError extends Error {
|
|
readonly statusCode: number;
|
|
readonly code: string;
|
|
readonly details?: unknown;
|
|
|
|
constructor(statusCode: number, code: string, message: string, details?: unknown) {
|
|
super(message);
|
|
this.name = 'HttpError';
|
|
this.statusCode = statusCode;
|
|
this.code = code;
|
|
this.details = details;
|
|
}
|
|
}
|
|
|
|
export class InvalidCredentialsError extends HttpError {
|
|
constructor() {
|
|
super(401, 'INVALID_CREDENTIALS', 'Invalid username or password');
|
|
}
|
|
}
|
|
|
|
export class UnauthorizedError extends HttpError {
|
|
constructor(message = 'Not authenticated') {
|
|
super(401, 'UNAUTHORIZED', message);
|
|
}
|
|
}
|
|
|
|
export class NotFoundError extends HttpError {
|
|
constructor(what = 'Resource') {
|
|
super(404, 'NOT_FOUND', `${what} not found`);
|
|
}
|
|
}
|
|
|
|
export class ConflictError extends HttpError {
|
|
constructor(message: string) {
|
|
super(409, 'CONFLICT', message);
|
|
}
|
|
}
|
|
|
|
export class ValidationError extends HttpError {
|
|
constructor(message: string, details?: unknown) {
|
|
super(400, 'VALIDATION', message, details);
|
|
}
|
|
}
|
|
|
|
export class ForbiddenError extends HttpError {
|
|
constructor(message = 'Forbidden') {
|
|
super(403, 'FORBIDDEN', message);
|
|
}
|
|
}
|