@noony-serverless/core
Version:
A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript
132 lines • 4.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceError = exports.InternalServerError = exports.ConflictError = exports.NotFoundError = exports.ForbiddenError = exports.UnauthorizedError = exports.TooLargeError = exports.TimeoutError = exports.SecurityError = exports.BusinessError = exports.AuthenticationError = exports.ValidationError = exports.HttpError = void 0;
class HttpError extends Error {
status;
code;
details;
constructor(status, message, code, details) {
super(message);
this.status = status;
this.code = code;
this.details = details;
this.name = 'HttpError';
}
}
exports.HttpError = HttpError;
class ValidationError extends HttpError {
constructor(message, details) {
super(400, message, 'VALIDATION_ERROR', details);
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
class AuthenticationError extends HttpError {
constructor(message = 'Unauthorized') {
super(401, message, 'AUTHENTICATION_ERROR');
this.name = 'AuthenticationError';
}
}
exports.AuthenticationError = AuthenticationError;
class BusinessError extends HttpError {
constructor(message, status = 500, details) {
super(status, message, 'BUSINESS_ERROR', details);
this.name = 'BusinessError';
}
}
exports.BusinessError = BusinessError;
class SecurityError extends HttpError {
constructor(message = 'Security violation detected', details) {
super(403, message, 'SECURITY_ERROR', details);
this.name = 'SecurityError';
}
}
exports.SecurityError = SecurityError;
class TimeoutError extends HttpError {
constructor(message = 'Request timeout', details) {
super(408, message, 'TIMEOUT_ERROR', details);
this.name = 'TimeoutError';
}
}
exports.TimeoutError = TimeoutError;
class TooLargeError extends HttpError {
constructor(message = 'Request entity too large', details) {
super(413, message, 'TOO_LARGE_ERROR', details);
this.name = 'TooLargeError';
}
}
exports.TooLargeError = TooLargeError;
/**
* 401 Unauthorized - Authentication required
* Alias for AuthenticationError for better semantic clarity
*/
class UnauthorizedError extends HttpError {
constructor(message = 'Authentication required') {
super(401, message, 'UNAUTHORIZED_ERROR');
this.name = 'UnauthorizedError';
}
}
exports.UnauthorizedError = UnauthorizedError;
/**
* 403 Forbidden - Insufficient permissions
* Use this for authorization failures (user is authenticated but lacks permission)
*/
class ForbiddenError extends HttpError {
constructor(message = 'Access denied', details) {
super(403, message, 'FORBIDDEN_ERROR', details);
this.name = 'ForbiddenError';
}
}
exports.ForbiddenError = ForbiddenError;
/**
* 404 Not Found - Resource not found
*/
class NotFoundError extends HttpError {
constructor(message = 'Resource not found', details) {
super(404, message, 'NOT_FOUND_ERROR', details);
this.name = 'NotFoundError';
}
}
exports.NotFoundError = NotFoundError;
/**
* 409 Conflict - Resource already exists or state conflict
*/
class ConflictError extends HttpError {
constructor(message = 'Resource already exists', details) {
super(409, message, 'CONFLICT_ERROR', details);
this.name = 'ConflictError';
}
}
exports.ConflictError = ConflictError;
/**
* 500 Internal Server Error - Unexpected errors with optional cause chaining
*/
class InternalServerError extends HttpError {
cause;
constructor(message = 'Internal server error', cause, details) {
super(500, message, 'INTERNAL_SERVER_ERROR', details);
this.cause = cause;
this.name = 'InternalServerError';
if (cause) {
this.stack = `${this.stack}\nCaused by: ${cause.stack}`;
}
}
}
exports.InternalServerError = InternalServerError;
/**
* Service layer error with error code
* Use this in service classes for business logic errors
* Not tied to specific HTTP status codes
*/
class ServiceError extends Error {
code;
details;
constructor(message, code, details) {
super(message);
this.code = code;
this.details = details;
this.name = 'ServiceError';
}
}
exports.ServiceError = ServiceError;
//# sourceMappingURL=errors.js.map