UNPKG

5.93 kBTypeScriptView Raw
1import { NamedError } from '@salesforce/kit';
2import { JsonMap, Optional } from '@salesforce/ts-types';
3import { Messages, Tokens } from './messages';
4/**
5 * A class to manage all the keys and tokens for a message bundle to use with SfdxError.
6 *
7 * ```
8 * SfdxError.create(new SfdxErrorConfig('MyPackage', 'apex', 'runTest').addAction('apexErrorAction1', [className]));
9 * ```
10 */
11export declare class SfdxErrorConfig {
12 /**
13 * The name of the package
14 */
15 readonly packageName: string;
16 /**
17 * The name of the bundle
18 */
19 readonly bundleName: string;
20 /**
21 * The error key
22 */
23 errorKey: string;
24 private errorTokens;
25 private messages?;
26 private actions;
27 /**
28 * Create a new SfdxErrorConfig.
29 *
30 * @param packageName The name of the package.
31 * @param bundleName The message bundle.
32 * @param errorKey The error message key.
33 * @param errorTokens The tokens to use when getting the error message.
34 * @param actionKey The action message key.
35 * @param actionTokens The tokens to use when getting the action message(s).
36 */
37 constructor(packageName: string, bundleName: string, errorKey: string, errorTokens?: Tokens, actionKey?: string, actionTokens?: Tokens);
38 /**
39 * Set the error key.
40 *
41 * @param key The key to set.
42 * @returns {SfdxErrorConfig} For convenience `this` object is returned.
43 */
44 setErrorKey(key: string): SfdxErrorConfig;
45 /**
46 * Set the error tokens.
47 *
48 * @param tokens The tokens to set. For convenience `this` object is returned.
49 */
50 setErrorTokens(tokens: Tokens): SfdxErrorConfig;
51 /**
52 * Add an error action to assist the user with a resolution. For convenience `this` object is returned.
53 *
54 * @param actionKey The action key in the message bundle.
55 * @param actionTokens The action tokens for the string.
56 */
57 addAction(actionKey: string, actionTokens?: Tokens): SfdxErrorConfig;
58 /**
59 * Load the messages using `Messages.loadMessages`. Returns the loaded messages.
60 */
61 load(): Messages;
62 /**
63 * Get the error message using messages.getMessage.
64 * **Throws** If `errorMessages.load` was not called first.
65 */
66 getError(): string;
67 /**
68 * Get the action messages using messages.getMessage.
69 * **@throws** If `errorMessages.load` was not called first.
70 */
71 getActions(): Optional<string[]>;
72 /**
73 * Remove all actions from this error config. Useful when reusing SfdxErrorConfig for other error messages within
74 * the same bundle. For convenience `this` object is returned.
75 */
76 removeActions(): SfdxErrorConfig;
77}
78/**
79 * A generalized sfdx error which also contains an action. The action is used in the
80 * CLI to help guide users past the error.
81 *
82 * To throw an error in a synchronous function you must either pass the error message and actions
83 * directly to the constructor, e.g.
84 *
85 * ```
86 * // To load a message bundle:
87 * Messages.importMessagesDirectory(__dirname);
88 * this.messages = Messages.loadMessages('myPackageName', 'myBundleName');
89 * // Note that __dirname should contain a messages folder.
90 *
91 * // To throw an error associated with the message from the bundle:
92 * throw SfdxError.create('myPackageName', 'myBundleName', 'MyErrorMessageKey', [messageToken1]);
93 *
94 * // To throw a non-bundle based error:
95 * throw new SfdxError(myErrMsg, 'MyErrorName');
96 * ```
97 */
98export declare class SfdxError extends NamedError {
99 /**
100 * The message string. Error.message
101 */
102 message: string;
103 name: any;
104 /**
105 * Action messages. Hints to the users regarding what can be done to fix related issues.
106 */
107 actions?: string[];
108 /**
109 * SfdxCommand can return this process exit code.
110 */
111 exitCode: number;
112 /**
113 * The related command name for this error.
114 */
115 commandName?: string;
116 data: any;
117 /**
118 * Some errors support `error.code` instead of `error.name`. This keeps backwards compatability.
119 */
120 private _code?;
121 /**
122 * Create an SfdxError.
123 *
124 * @param message The error message.
125 * @param name The error name. Defaults to 'SfdxError'.
126 * @param actions The action message(s).
127 * @param exitCode The exit code which will be used by SfdxCommand.
128 * @param cause The underlying error that caused this error to be raised.
129 */
130 constructor(message: string, name?: string, actions?: string[], exitCode?: number, cause?: Error);
131 /**
132 * Create a new `SfdxError`.
133 *
134 * @param packageName The message package name used to create the `SfdxError`.
135 * @param bundleName The message bundle name used to create the `SfdxError`.
136 * @param key The key within the bundle for the message.
137 * @param tokens The values to use for message tokenization.
138 */
139 static create(packageName: string, bundleName: string, key: string, tokens?: Tokens): SfdxError;
140 /**
141 * Create a new SfdxError.
142 *
143 * @param errorConfig The `SfdxErrorConfig` object used to create the SfdxError.
144 */
145 static create(errorConfig: SfdxErrorConfig): SfdxError;
146 /**
147 * Convert an Error to an SfdxError.
148 *
149 * @param err The error to convert.
150 */
151 static wrap(err: Error | string): SfdxError;
152 get code(): string | undefined | any;
153 set code(code: string);
154 /**
155 * Sets the name of the command. For convenience `this` object is returned.
156 *
157 * @param commandName The command name.
158 */
159 setCommandName(commandName: string): SfdxError;
160 /**
161 * An additional payload for the error. For convenience `this` object is returned.
162 *
163 * @param data The payload data.
164 */
165 setData(data: unknown): SfdxError;
166 /**
167 * Convert an {@link SfdxError} state to an object. Returns a plain object representing the state of this error.
168 */
169 toObject(): JsonMap;
170}