1 | ;
|
2 | /*
|
3 | * Copyright (c) 2020, salesforce.com, inc.
|
4 | * All rights reserved.
|
5 | * Licensed under the BSD 3-Clause license.
|
6 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
7 | */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.SfdxError = exports.SfError = void 0;
|
10 | const kit_1 = require("@salesforce/kit");
|
11 | const ts_types_1 = require("@salesforce/ts-types");
|
12 | /**
|
13 | * A generalized sfdx error which also contains an action. The action is used in the
|
14 | * CLI to help guide users past the error.
|
15 | *
|
16 | * To throw an error in a synchronous function you must either pass the error message and actions
|
17 | * directly to the constructor, e.g.
|
18 | *
|
19 | * ```
|
20 | * // To load a message bundle (Note that __dirname should contain a messages folder)
|
21 | * Messages.importMessagesDirectory(__dirname);
|
22 | * const messages = Messages.load('myPackageName', 'myBundleName');
|
23 | *
|
24 | * // To throw a non-bundle based error:
|
25 | * throw new SfError(message.getMessage('myError'), 'MyErrorName');
|
26 | * ```
|
27 | */
|
28 | class SfError extends kit_1.NamedError {
|
29 | /**
|
30 | * Create an SfError.
|
31 | *
|
32 | * @param message The error message.
|
33 | * @param name The error name. Defaults to 'SfError'.
|
34 | * @param actions The action message(s).
|
35 | * @param exitCodeOrCause The exit code which will be used by SfdxCommand or he underlying error that caused this error to be raised.
|
36 | * @param cause The underlying error that caused this error to be raised.
|
37 | */
|
38 | constructor(message, name, actions, exitCodeOrCause, cause) {
|
39 | cause = exitCodeOrCause instanceof Error ? exitCodeOrCause : cause;
|
40 | super(name || 'SfError', message || name, cause);
|
41 | this.actions = actions;
|
42 | if (typeof exitCodeOrCause === 'number') {
|
43 | this.exitCode = exitCodeOrCause;
|
44 | }
|
45 | else {
|
46 | this.exitCode = 1;
|
47 | }
|
48 | }
|
49 | /**
|
50 | * Convert an Error to an SfError.
|
51 | *
|
52 | * @param err The error to convert.
|
53 | */
|
54 | static wrap(err) {
|
55 | if ((0, ts_types_1.isString)(err)) {
|
56 | return new SfError(err);
|
57 | }
|
58 | if (err instanceof SfError) {
|
59 | return err;
|
60 | }
|
61 | const sfError = new SfError(err.message, err.name, undefined, err);
|
62 | // If the original error has a code, use that instead of name.
|
63 | if ((0, ts_types_1.hasString)(err, 'code')) {
|
64 | sfError.code = err.code;
|
65 | }
|
66 | return sfError;
|
67 | }
|
68 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
69 | get code() {
|
70 | return this._code || this.name;
|
71 | }
|
72 | set code(code) {
|
73 | this._code = code;
|
74 | }
|
75 | /**
|
76 | * Sets the context of the error. For convenience `this` object is returned.
|
77 | *
|
78 | * @param context The command name.
|
79 | */
|
80 | setContext(context) {
|
81 | this.context = context;
|
82 | return this;
|
83 | }
|
84 | /**
|
85 | * An additional payload for the error. For convenience `this` object is returned.
|
86 | *
|
87 | * @param data The payload data.
|
88 | */
|
89 | setData(data) {
|
90 | this.data = data;
|
91 | return this;
|
92 | }
|
93 | /**
|
94 | * Convert an {@link SfError} state to an object. Returns a plain object representing the state of this error.
|
95 | */
|
96 | toObject() {
|
97 | const obj = {
|
98 | name: this.name,
|
99 | message: this.message || this.name,
|
100 | exitCode: this.exitCode,
|
101 | actions: this.actions,
|
102 | };
|
103 | if (this.context) {
|
104 | obj.context = this.context;
|
105 | }
|
106 | if (this.data) {
|
107 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
108 | obj.data = this.data;
|
109 | }
|
110 | return obj;
|
111 | }
|
112 | }
|
113 | exports.SfError = SfError;
|
114 | /**
|
115 | * @deprecated use SfError instead
|
116 | */
|
117 | class SfdxError extends SfError {
|
118 | }
|
119 | exports.SfdxError = SfdxError;
|
120 | //# sourceMappingURL=sfError.js.map |
\ | No newline at end of file |