70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
import { assertError, isError } from "./error.js";
|
|
import { getGlobalName } from "./global.js";
|
|
import { parseArguments } from "./tools.js";
|
|
export class Layerr extends Error {
|
|
constructor(errorOptionsOrMessage, messageText) {
|
|
const args = [...arguments];
|
|
const { options, shortMessage } = parseArguments(args);
|
|
let message = shortMessage;
|
|
if (options.cause) {
|
|
message = `${message}: ${options.cause.message}`;
|
|
}
|
|
super(message);
|
|
this.message = message;
|
|
if (options.name && typeof options.name === "string") {
|
|
this.name = options.name;
|
|
}
|
|
else {
|
|
this.name = getGlobalName();
|
|
}
|
|
if (options.cause) {
|
|
Object.defineProperty(this, "_cause", { value: options.cause });
|
|
}
|
|
Object.defineProperty(this, "_info", { value: {} });
|
|
if (options.info && typeof options.info === "object") {
|
|
Object.assign(this._info, options.info);
|
|
}
|
|
if (Error.captureStackTrace) {
|
|
const ctor = options.constructorOpt || this.constructor;
|
|
Error.captureStackTrace(this, ctor);
|
|
}
|
|
}
|
|
static cause(err) {
|
|
assertError(err);
|
|
if (!err._cause)
|
|
return null;
|
|
return isError(err._cause)
|
|
? err._cause
|
|
: null;
|
|
}
|
|
static fullStack(err) {
|
|
assertError(err);
|
|
const cause = Layerr.cause(err);
|
|
if (cause) {
|
|
return `${err.stack}\ncaused by: ${Layerr.fullStack(cause)}`;
|
|
}
|
|
return err.stack ?? "";
|
|
}
|
|
static info(err) {
|
|
assertError(err);
|
|
const output = {};
|
|
const cause = Layerr.cause(err);
|
|
if (cause) {
|
|
Object.assign(output, Layerr.info(cause));
|
|
}
|
|
if (err._info) {
|
|
Object.assign(output, err._info);
|
|
}
|
|
return output;
|
|
}
|
|
toString() {
|
|
let output = this.name ||
|
|
this.constructor.name ||
|
|
this.constructor.prototype.name;
|
|
if (this.message) {
|
|
output = `${output}: ${this.message}`;
|
|
}
|
|
return output;
|
|
}
|
|
}
|