UNPKG

@salesforce/core

Version:

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

120 lines 3.89 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 = 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 SfdxError(message.getMessage('myError'), 'MyErrorName'); * ``` */ class SfdxError extends kit_1.NamedError { /** * Create an SfdxError. * * @param message The error message. * @param name The error name. Defaults to 'SfdxError'. * @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 || 'SfdxError', message || name, cause); this.actions = actions; if (typeof exitCodeOrCause === 'number') { this.exitCode = exitCodeOrCause; } else { this.exitCode = 1; } } /** * Convert an Error to an SfdxError. * * @param err The error to convert. */ static wrap(err) { if (ts_types_1.isString(err)) { return new SfdxError(err); } if (err instanceof SfdxError) { return err; } const sfdxError = new SfdxError(err.message, err.name, undefined, err); // If the original error has a code, use that instead of name. if (ts_types_1.hasString(err, 'code')) { sfdxError.code = err.code; } return sfdxError; } 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 SfdxError} 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; } /** * @deprecated Does nothing. Do not use. This is kept around to support older versions of SfdxCommand. * @param commandName */ setCommandName() { /** Do nothing. */ } } exports.SfdxError = SfdxError; //# sourceMappingURL=sfdxError.js.map