@nasriya/cachify
Version:
A lightweight, extensible in-memory caching library for storing anything, with built-in TTL and customizable cache types.
113 lines (112 loc) • 3.36 kB
JavaScript
export class EngineError extends Error {
#_data = {
type: Error,
message: '',
stack: undefined,
cause: undefined,
errors: [],
};
#_helpers = {
type: {
getType: (err) => {
if (err instanceof TypeError) {
return TypeError;
}
if (err instanceof SyntaxError) {
return SyntaxError;
}
if (err instanceof ReferenceError) {
return ReferenceError;
}
if (err instanceof RangeError) {
return RangeError;
}
if (err instanceof EvalError) {
return EvalError;
}
if (err instanceof URIError) {
return URIError;
}
if (err instanceof AggregateError) {
return AggregateError;
}
return Error;
}
},
updateFrom: (err) => {
this.#_data.type = this.#_helpers.type.getType(err);
this.message = err.message;
this.stack = err.stack;
this.cause = err.cause;
this.errors = err instanceof AggregateError ? err.errors : [];
}
};
constructor(message) {
super(message);
this.#_data.message = message;
}
from(error) {
this.#_helpers.updateFrom(error);
}
/**
* Retrieves the array of errors that triggered this EngineError.
*
* @returns {any[]} The array of errors associated with this EngineError.
*/
get errors() { return this.#_data.errors; }
/**
* Sets the array of errors that triggered this error.
*
* @param {any[]} value - The array of errors that triggered this error.
*/
set errors(value) { this.#_data.errors = value; }
/**
* Retrieves the underlying cause of this error, if available.
*
* @returns {unknown} The cause of the error, which may be any data type or undefined if no cause is set.
*/
get cause() { return this.#_data.cause; }
/**
* Sets the underlying cause of this error.
*
* @param {unknown} value The cause of the error, which may be any data type or undefined if no cause is set.
*/
set cause(value) {
this.#_data.cause = value;
}
/**
* The name of the error.
* @readonly
* @type {'EngineError'}
*/
get name() { return 'EngineError'; }
/**
* The error message associated with this EngineError.
* @readonly
* @type {string}
*/
get message() { return this.#_data.message; }
/**
* Sets the error message associated with this EngineError.
* @param {string} value The error message.
*/
set message(value) {
this.#_data.message = value;
super.message = value;
}
/**
* The stack trace of the error.
* @readonly
* @type {string | undefined}
*/
get stack() { return this.#_data.stack; }
/**
* Sets the stack trace of the error.
* @param {string | undefined} value The error stack trace.
*/
set stack(value) {
this.#_data.stack = value;
super.stack = value;
}
}
export default EngineError;