UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

120 lines 3.85 kB
"use strict"; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SfdxError = exports.SfError = void 0; const kit_1 = require("@salesforce/kit"); const ts_types_1 = require("@salesforce/ts-types"); /** * A generalized sfdx error which also contains an action. The action is used in the * CLI to help guide users past the error. * * To throw an error in a synchronous function you must either pass the error message and actions * directly to the constructor, e.g. * * ``` * // To load a message bundle (Note that __dirname should contain a messages folder) * Messages.importMessagesDirectory(__dirname); * const messages = Messages.load('myPackageName', 'myBundleName'); * * // To throw a non-bundle based error: * throw new SfError(message.getMessage('myError'), 'MyErrorName'); * ``` */ class SfError extends kit_1.NamedError { /** * Create an SfError. * * @param message The error message. * @param name The error name. Defaults to 'SfError'. * @param actions The action message(s). * @param exitCodeOrCause The exit code which will be used by SfdxCommand or he underlying error that caused this error to be raised. * @param cause The underlying error that caused this error to be raised. */ constructor(message, name, actions, exitCodeOrCause, cause) { cause = exitCodeOrCause instanceof Error ? exitCodeOrCause : cause; super(name || 'SfError', message || name, cause); this.actions = actions; if (typeof exitCodeOrCause === 'number') { this.exitCode = exitCodeOrCause; } else { this.exitCode = 1; } } /** * Convert an Error to an SfError. * * @param err The error to convert. */ static wrap(err) { if ((0, ts_types_1.isString)(err)) { return new SfError(err); } if (err instanceof SfError) { return err; } const sfError = new SfError(err.message, err.name, undefined, err); // If the original error has a code, use that instead of name. if ((0, ts_types_1.hasString)(err, 'code')) { sfError.code = err.code; } return sfError; } // eslint-disable-next-line @typescript-eslint/no-explicit-any get code() { return this._code || this.name; } set code(code) { this._code = code; } /** * Sets the context of the error. For convenience `this` object is returned. * * @param context The command name. */ setContext(context) { this.context = context; return this; } /** * An additional payload for the error. For convenience `this` object is returned. * * @param data The payload data. */ setData(data) { this.data = data; return this; } /** * Convert an {@link SfError} state to an object. Returns a plain object representing the state of this error. */ toObject() { const obj = { name: this.name, message: this.message || this.name, exitCode: this.exitCode, actions: this.actions, }; if (this.context) { obj.context = this.context; } if (this.data) { // eslint-disable-next-line @typescript-eslint/no-explicit-any obj.data = this.data; } return obj; } } exports.SfError = SfError; /** * @deprecated use SfError instead */ class SfdxError extends SfError { } exports.SfdxError = SfdxError; //# sourceMappingURL=sfError.js.map