UNPKG

restifyx.js

Version:

Advanced API endpoint handler system with automatic documentation

68 lines 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiError = void 0; class ApiError extends Error { constructor(statusCode, message, details, errorCode) { super(message); this.statusCode = statusCode; this.details = details; this.errorCode = errorCode; this.timestamp = Date.now(); this.name = this.constructor.name; Error.captureStackTrace(this, this.constructor); } static badRequest(message, details, errorCode) { return new ApiError(400, message, details, errorCode || "BAD_REQUEST"); } static unauthorized(message = "Unauthorized", details, errorCode) { return new ApiError(401, message, details, errorCode || "UNAUTHORIZED"); } static forbidden(message = "Forbidden", details, errorCode) { return new ApiError(403, message, details, errorCode || "FORBIDDEN"); } static notFound(message = "Not found", details, errorCode) { return new ApiError(404, message, details, errorCode || "NOT_FOUND"); } static conflict(message, details, errorCode) { return new ApiError(409, message, details, errorCode || "CONFLICT"); } static validation(message, details, errorCode) { return new ApiError(422, message, details, errorCode || "VALIDATION_ERROR"); } static tooManyRequests(message = "Too many requests", details, errorCode) { return new ApiError(429, message, details, errorCode || "RATE_LIMIT_EXCEEDED"); } static internal(message = "Internal server error", details, errorCode) { return new ApiError(500, message, details, errorCode || "INTERNAL_ERROR"); } static serviceUnavailable(message = "Service unavailable", details, errorCode) { return new ApiError(503, message, details, errorCode || "SERVICE_UNAVAILABLE"); } static badGateway(message = "Bad gateway", details, errorCode) { return new ApiError(502, message, details, errorCode || "BAD_GATEWAY"); } static gatewayTimeout(message = "Gateway timeout", details, errorCode) { return new ApiError(504, message, details, errorCode || "GATEWAY_TIMEOUT"); } static methodNotAllowed(message = "Method not allowed", details, errorCode) { return new ApiError(405, message, details, errorCode || "METHOD_NOT_ALLOWED"); } static payloadTooLarge(message = "Payload too large", details, errorCode) { return new ApiError(413, message, details, errorCode || "PAYLOAD_TOO_LARGE"); } static unsupportedMediaType(message = "Unsupported media type", details, errorCode) { return new ApiError(415, message, details, errorCode || "UNSUPPORTED_MEDIA_TYPE"); } toJSON() { return { status: "error", statusCode: this.statusCode, message: this.message, errorCode: this.errorCode, details: this.details, timestamp: this.timestamp, }; } } exports.ApiError = ApiError; //# sourceMappingURL=ApiError.js.map