@openshift-console/dynamic-plugin-sdk
Version:
Provides core APIs, types and utilities used by dynamic plugins at runtime.
48 lines (47 loc) • 1.6 kB
JavaScript
/**
* Allows to easily extend a base class to create custom applicative errors.
*
* example:
* ```
* class HttpError extends CustomError {
* public constructor(
* public code: number,
* message?: string,
* ) {
* super(message)
* }
* }
*
* new HttpError(404, 'Not found')
* ```
*/
export class CustomError extends Error {
constructor(message) {
super(message);
// set error name as constructor name, make it not enumerable to keep native Error behavior
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors
Object.defineProperty(this, 'name', {
value: new.target.name,
enumerable: false,
configurable: true,
});
// Use captureStackTrace when available to remove contructor from stack trace
// Add message to the stack trace
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
}
else {
this.stack = new Error(message).stack;
}
// fix the extended error prototype chain
// because typescript __extends implementation can't
// see https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, new.target.prototype);
}
}
export class ErrorWithCause extends CustomError {
constructor(message, cause) {
super(message);
this.cause = cause;
}
}