glitchkit
Version:
A lightweight toolkit to create and manage expressive, structured, and reusable error types. Perfect for APIs, services, and glitchy adventures
87 lines (86 loc) • 2.49 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const GlitchKitMetadata_1 = __importDefault(require("../utility/GlitchKitMetadata"));
const GlitchKitTraceability_1 = __importDefault(require("../utility/GlitchKitTraceability"));
/**
* Base class for all GlitchKit errors.
* Provides common properties and methods for error handling.
*/
class GlitchKitBaseError extends Error {
constructor(message, errorCode) {
super(message);
// Properties for traceability and metadata
this._traceability = new GlitchKitTraceability_1.default();
this._metadata = new GlitchKitMetadata_1.default();
this._errorCode = errorCode;
this._timestamp = new Date();
Object.setPrototypeOf(this, GlitchKitBaseError.prototype);
Error.captureStackTrace(this, this.constructor);
}
get traceability() {
return this._traceability;
}
set traceability(trace) {
this._traceability = trace;
}
withTraceability(trace) {
this._traceability = trace;
return this;
}
get metadata() {
return this._metadata;
}
set metadata(meta) {
this._metadata = meta;
}
withMetadata(meta) {
this._metadata = meta;
return this;
}
get errorCode() {
return this._errorCode;
}
set errorCode(code) {
this._errorCode = code;
}
withErrorCode(code) {
this._errorCode = code;
return this;
}
get timestamp() {
return this._timestamp;
}
get durationMs() {
return this._durationMs;
}
set durationMs(val) {
this._durationMs = val;
}
withDurationMs(val) {
this._durationMs = val;
return this;
}
static isInstance(error) {
return error instanceof GlitchKitBaseError;
}
toJSON() {
const jsonError = {
name: this.name,
message: this.message,
errorCode: this._errorCode,
stack: this.stack,
timestamp: this._timestamp.toISOString(),
durationMs: this._durationMs,
metadata: this.metadata.toJson(),
traceability: this.traceability.toJson(),
};
return jsonError;
}
raise() {
throw this;
}
}
exports.default = GlitchKitBaseError;