@mos-connection/helper
Version:
Helper functions for the MOS-connection library
73 lines (72 loc) • 2.09 kB
JavaScript
;
/*
Typical use case:
function (xml) {
try {
// do something with xml.storyBody
} catch (e) {
throw ParseError.handleCaughtError('storyBody', e)
}
}
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParseError = void 0;
class ParseError {
static handleCaughtError(basePath, e) {
if (ParseError.isParseError(e)) {
e.addBreadcrumb(basePath);
return e;
}
else if (e instanceof Error) {
return ParseError.fromError(e, basePath);
}
else if (typeof e === 'string') {
return new ParseError(basePath, e);
}
else {
return new ParseError(basePath, `${e}`, e.stack);
}
}
static isParseError(e) {
return e instanceof ParseError;
}
static fromError(e, path) {
const pe = new ParseError(path, e.message, e.stack);
pe.stack = e.stack;
return pe;
}
static handleError(func, path) {
try {
return func();
}
catch (org) {
throw this.handleCaughtError(path, org);
}
}
constructor(path, message, stack) {
this.name = 'ParseError';
this.stack = undefined;
this.orgStack = undefined;
this.breadcrumbs = [];
this.orgMessage = message.replace(/^Error: /, '');
this.message = ''; // Updated in updateVars()
this.orgStack = `${stack ?? new Error(message).stack}`.replace(/^Error: /, '');
this.breadcrumbs.push(path);
this.updateVars();
}
addBreadcrumb(path) {
if (path && path !== this.breadcrumbs[0]) {
this.breadcrumbs.unshift(path);
this.updateVars();
}
}
updateVars() {
this.message = `ParseError: ${this.breadcrumbs.join('.')}: ${this.orgMessage}`;
this.stack = `ParseError: ${this.breadcrumbs.join('.')}: ${this.orgStack}`;
}
toString() {
return this.message;
}
}
exports.ParseError = ParseError;
//# sourceMappingURL=ParseError.js.map