UNPKG

@othree.io/excuses

Version:

Excuses

39 lines 1.53 kB
/** * Generates a JSON string containing error details. * * @param {string} [code] - Optional error code. * @param {string} [message] - Optional error message. If not provided, defaults to `UnexpectedError.ERROR`. * @returns {string} A JSON string with `code` and `message` properties. */ const getErrorDetails = (code, message) => { return { code, message: message ? message : UnexpectedError.ERROR }; }; /** * Custom Error class representing an unexpected error. */ export class UnexpectedError extends Error { /** Static error code to identify the error type. */ static ERROR = 'UnexpectedError'; /** Error code associated with this error instance. */ code; errorDetails; /** * Creates a new UnexpectedError. * * @param {string} [code] - Optional error code. * @param {string} [message] - Optional error message. If not provided, a default error message is generated by `getErrorJSONString`. */ constructor(code, message) { super(JSON.stringify(getErrorDetails(code, message))); // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain this.name = UnexpectedError.ERROR; this.stack = new Error().stack; this.code = code; this.errorDetails = getErrorDetails(code, message); } } //# sourceMappingURL=unexpected-error.js.map