@nowarajs/error
Version:
A collection of error classes for NowaraJS
50 lines (47 loc) • 1.16 kB
JavaScript
// @bun
import {
HTTP_STATUS_CODES
} from "./chunk-wfrmgvww.js";
// source/baseError.ts
import { randomUUID } from "crypto";
class BaseError extends Error {
cause;
_uuid = randomUUID();
_date = new Date;
constructor(options) {
super(options?.message, {
cause: options?.cause
});
super.name = "BaseError";
this.cause = options?.cause;
}
get uuid() {
return this._uuid;
}
get date() {
return this._date;
}
}
// source/httpError.ts
class HttpError extends BaseError {
_httpStatusCode;
constructor(options) {
super(options);
super.name = "HttpError";
const statusCodeOption = options?.httpStatusCode;
this._httpStatusCode = typeof statusCodeOption === "number" ? statusCodeOption : HTTP_STATUS_CODES[statusCodeOption ?? "INTERNAL_SERVER_ERROR"] ?? HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR;
}
get httpStatusCode() {
return this._httpStatusCode;
}
get isClientError() {
return this._httpStatusCode >= 400 && this._httpStatusCode < 500;
}
get isServerError() {
return this._httpStatusCode >= 500 && this._httpStatusCode < 600;
}
}
export {
HttpError,
BaseError
};