@lodestar/utils
Version:
Utilities required across multiple lodestar packages
59 lines • 1.45 kB
JavaScript
/**
* Generic Lodestar error with attached metadata
*/
export class LodestarError extends Error {
type;
constructor(type, message, stack) {
super(message || type.code);
this.type = type;
if (stack)
this.stack = stack;
}
getMetadata() {
return this.type;
}
/**
* Get the metadata and the stacktrace for the error.
*/
toObject() {
return {
type: this.getMetadata(),
message: this.message ?? "",
stack: this.stack ?? "",
className: this.constructor.name,
};
}
static fromObject(obj) {
return new LodestarError(obj.type, obj.message, obj.stack);
}
}
/**
* Throw this error when an upstream abort signal aborts
*/
export class ErrorAborted extends Error {
constructor(message) {
super(`Aborted ${message || ""}`);
}
}
/**
* Throw this error when wrapped timeout expires
*/
export class TimeoutError extends Error {
constructor(message) {
super(`Timeout ${message || ""}`);
}
}
/**
* Returns true if arg `e` is an instance of `ErrorAborted`
*/
export function isErrorAborted(e) {
return e instanceof ErrorAborted;
}
/**
* Extend an existing error by appending a string to its `e.message`
*/
export function extendError(e, appendMessage) {
e.message = `${e.message} - ${appendMessage}`;
return e;
}
//# sourceMappingURL=errors.js.map