UNPKG

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