@othree.io/excuses
Version:
Excuses
40 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConflictError = void 0;
/**
* 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 : ConflictError.ERROR
};
};
/**
* Custom Error class representing an unexpected error.
*/
class ConflictError extends Error {
/**
* Creates a new ConflictError.
*
* @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 = ConflictError.ERROR;
this.stack = new Error().stack;
this.code = code;
this.errorDetails = getErrorDetails(code, message);
}
}
exports.ConflictError = ConflictError;
/** Static error code to identify the error type. */
ConflictError.ERROR = 'ConflictError';
//# sourceMappingURL=conflict-error.js.map