@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
131 lines • 5.14 kB
JavaScript
"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
*/
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _SfError_code;
Object.defineProperty(exports, "__esModule", { value: true });
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);
/**
* Some errors support `error.code` instead of `error.name`. This keeps backwards compatability.
*/
_SfError_code.set(this, void 0);
this.actions = actions;
if (typeof exitCodeOrCause === 'number') {
this.exitCode = exitCodeOrCause;
}
else {
this.exitCode = 1;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get code() {
return __classPrivateFieldGet(this, _SfError_code, "f") ?? this.name;
}
set code(code) {
__classPrivateFieldSet(this, _SfError_code, code, "f");
}
/**
* 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;
}
/**
* 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, @typescript-eslint/no-unsafe-assignment
obj.data = this.data;
}
return obj;
}
}
exports.SfError = SfError;
_SfError_code = new WeakMap();
//# sourceMappingURL=sfError.js.map