47 lines
975 B
JavaScript
47 lines
975 B
JavaScript
/**
|
|
* Custom Error Classes
|
|
* For structured error handling with proper status codes
|
|
*/
|
|
|
|
class AppError extends Error {
|
|
constructor(message, statusCode = 500, code = 'INTERNAL_ERROR') {
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
this.code = code;
|
|
this.name = this.constructor.name;
|
|
Error.captureStackTrace(this, this.constructor);
|
|
}
|
|
}
|
|
|
|
class ValidationError extends AppError {
|
|
constructor(message) {
|
|
super(message, 400, 'VALIDATION_ERROR');
|
|
}
|
|
}
|
|
|
|
class NotFoundError extends AppError {
|
|
constructor(resource) {
|
|
super(`${resource} not found`, 404, 'NOT_FOUND');
|
|
}
|
|
}
|
|
|
|
class UnauthorizedError extends AppError {
|
|
constructor(message = 'Unauthorized') {
|
|
super(message, 401, 'UNAUTHORIZED');
|
|
}
|
|
}
|
|
|
|
class ForbiddenError extends AppError {
|
|
constructor(message = 'Forbidden') {
|
|
super(message, 403, 'FORBIDDEN');
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
AppError,
|
|
ValidationError,
|
|
NotFoundError,
|
|
UnauthorizedError,
|
|
ForbiddenError,
|
|
};
|