@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
84 lines (80 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* A generic exception class, which can be used to represent any kind of error.
* It's similar to the `DOMException`, but for any JavaScript environment.
*
* @example
* ```ts
* // throw an exception with a name
* import { Exception } from "@ayonli/jsext/error";
*
* throw new Exception("The resource cannot be found", "NotFoundError");
* ```
*
* @example
* ```ts
* // throw an exception with a code
* import { Exception } from "@ayonli/jsext/error";
*
* throw new Exception("The resource cannot be found", 404);
* ```
*
* @example
* ```ts
* // rethrow an exception with a cause
* import { Exception } from "@ayonli/jsext/error";
*
* try {
* throw new Error("Something went wrong");
* } catch (error) {
* throw new Exception("An error occurred", { cause: error });
* }
* ```
*/
class Exception extends Error {
constructor(message, options = 0) {
super(message);
this.code = 0;
if (typeof options === "number") {
this.code = options;
}
else if (typeof options === "string") {
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
writable: true,
value: options,
});
}
else {
if (options.name) {
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
writable: true,
value: options.name,
});
}
if (options.cause) {
Object.defineProperty(this, "cause", {
configurable: true,
enumerable: false,
writable: true,
value: options.cause,
});
}
if (options.code) {
this.code = options.code;
}
}
}
}
Object.defineProperty(Exception.prototype, "name", {
configurable: true,
enumerable: false,
writable: true,
value: "Exception",
});
exports.default = Exception;
//# sourceMappingURL=Exception.js.map