exstack
Version:
A utility library designed to simplify and enhance Express.js applications.
123 lines (121 loc) • 3.74 kB
JavaScript
import { HttpStatus } from "../status.js";
//#region src/helps/http-error.ts
/**
* Get a human-readable error name from the HTTP status code.
* @param {number} status - The HTTP status code.
* @returns {string} - The formatted error name.
*/
const getErrorName = (status) => {
if (status < 400 || status > 511) return "HttpError";
const statusKey = HttpStatus[`${status}_NAME`];
if (!statusKey) return "HttpError";
const name = statusKey.toLowerCase().replace(/_/g, " ").replace(/\b\w/g, (char) => char.toUpperCase()).replace(/\s+/g, "");
return name.endsWith("Error") ? name : name.concat("Error");
};
/**
* Base class for handling HTTP errors.
* @extends {Error}
*/
var HttpError = class HttpError extends Error {
/**
* Creates an instance of `HTTPException`.
* @param status - HTTP status code for the exception. Defaults to 500.
* @param options - Additional options for the exception.
*/
constructor(status = HttpStatus.INTERNAL_SERVER_ERROR, options) {
super(typeof options.message === "string" ? options.message : getErrorName(status));
this.status = status;
this.options = options;
this.name = getErrorName(status);
Error.captureStackTrace(this, this.constructor);
}
/**
* Check if the given error is an instance of HttpError.
* @param {unknown} value - The error to check.
* @returns {boolean} - True if the error is an instance of HttpError, false otherwise.
*
* @example
* if (HttpError.isHttpError(error)) {
* // Handle the HttpError
* }
*/
static isHttpError = (value) => value instanceof HttpError;
/**
* Convert the HttpError instance to a Body object.
* @example
* const errorBody = new HttpError(404, {message: 'Not Found'}).body;
*/
get body() {
const { name: error, status } = this;
const { message, data = null, code = null } = this.options;
return {
status,
error,
message,
data,
code
};
}
/**
* Send the json of the error in an HTTP response.
* @param {Response} res - The Express response object.
*
* @example
* new HttpError(404, {message: 'Not Found'}).toJson(res);
*/
toJson = (res) => {
res.status(this.status).json(this.body);
};
};
/**
* Utility function to create custom error classes.
* @param status - HTTP status code.
* @returns - A new error class.
* @example
* const NotFoundError = createHttpErrorClass(HttpStatus.NOT_FOUND);
*/
const createHttpErrorClass = (status) => class extends HttpError {
constructor(message, options = {}) {
super(status, {
message,
...options
});
}
};
/**
* Represents a Bad Request HTTP error (400).
* @extends {HttpError}
*/
const BadRequestError = createHttpErrorClass(HttpStatus.BAD_REQUEST);
/**
* Represents a Conflict HTTP error (409).
* @extends {HttpError}
*/
const ConflictError = createHttpErrorClass(HttpStatus.CONFLICT);
/**
* Represents a Forbidden HTTP error (403).
* @extends {HttpError}
*/
const ForbiddenError = createHttpErrorClass(HttpStatus.FORBIDDEN);
/**
* Represents a Not Found HTTP error (404).
* @extends {HttpError}
*/
const NotFoundError = createHttpErrorClass(HttpStatus.NOT_FOUND);
/**
* Represents an UnAuthorized HTTP error (401).
* @extends {HttpError}
*/
const UnAuthorizedError = createHttpErrorClass(HttpStatus.UNAUTHORIZED);
/**
* Represents an Internal Server Error HTTP error (500).
* @extends {HttpError}
*/
const InternalServerError = createHttpErrorClass(HttpStatus.INTERNAL_SERVER_ERROR);
/**
* Represents an Content Too Larger Error HTTP error (413).
* @extends {HttpError}
*/
const ContentTooLargeError = createHttpErrorClass(HttpStatus.PAYLOAD_TOO_LARGE);
//#endregion
export { BadRequestError, ConflictError, ContentTooLargeError, ForbiddenError, HttpError, InternalServerError, NotFoundError, UnAuthorizedError, createHttpErrorClass };