UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

95 lines (88 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompositeError = void 0; function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } var _nestedErrors = /*#__PURE__*/new WeakMap(); /** This type is returnd by the CompositeError.toDictionary() method. Intended to be outputted to JSON or YAML. */ /** Error object which can provide nested errors to indicated if multiple errors have occurred. */ class CompositeError extends Error { /** * Creates a new error wrapping the given array of errors. * @param errors Array of errors to be owned by this composite error. Can contain CompositeError instances. * @param message Optional error message to be displayed by this root error object. */ constructor(errors, message) { super(message); /** Array of errors wrapped by this error object. */ _classPrivateFieldInitSpec(this, _nestedErrors, void 0); _classPrivateFieldSet(_nestedErrors, this, errors); } /** Gets the name of this error type. */ get name() { return 'CompositeError'; } /** Gets an array of error objects owned by this composite error. */ get nestedErrors() { return _classPrivateFieldGet(_nestedErrors, this); } /** * Creates a dictionary providing the name, message, and array of all errors nested under this error object. * Intended to be outputted to JSON or YAML. * @returns Returns a dictionary of this error object and all nested error objects. */ toDictionary() { const dictionary = { name: this.name, message: this.message, nestedErrors: [] }; for (const nextError of _classPrivateFieldGet(_nestedErrors, this)) { if (nextError instanceof CompositeError) { dictionary.nestedErrors.push(nextError.toDictionary()); } else { dictionary.nestedErrors.push({ name: nextError.name, message: nextError.message }); } } return dictionary; } /** * Creates an array of strings providing this object's error message and all of its nested error messages. * Each error object's message is added to its own array entry and will be indented according to nested position. * @returns Returns an array of all error messages that are indented according to their nested position. */ toNestedMessageArray() { const messageLines = []; if (this.message) { messageLines.push(this.message); } for (const nextError of _classPrivateFieldGet(_nestedErrors, this)) { if (nextError instanceof CompositeError) { for (const nextLine of nextError.toNestedMessageArray()) { const prefix = nextLine.startsWith('-') ? ' ' : '* '; messageLines.push(prefix + nextLine); } } else { messageLines.push(`- ${nextError.message || 'Unknown error'}`); } } return messageLines; } /** * Creates an error message containing this object's message and all nested error messages separated by newlines * and indented according to their nested position. * @returns Returns a single string containing all nested error messages separated by newlines. */ toNestedMessageString() { return this.toNestedMessageArray().join('\n'); } } exports.CompositeError = CompositeError;