UNPKG

3.2 kBTypeScriptView Raw
1import { AnyJson } from '@salesforce/ts-types';
2export type SfErrorOptions<T extends ErrorDataProperties = ErrorDataProperties> = {
3 message: string;
4 exitCode?: number;
5 name?: string;
6 data?: T;
7 /** pass an Error. For convenience in catch blocks, code will check that it is, in fact, an Error */
8 cause?: unknown;
9 context?: string;
10 actions?: string[];
11};
12type ErrorDataProperties = AnyJson;
13type SfErrorToObjectResult = {
14 name: string;
15 message: string;
16 exitCode: number;
17 actions?: string[];
18 context?: string;
19 data?: ErrorDataProperties;
20};
21/**
22 * A generalized sfdx error which also contains an action. The action is used in the
23 * CLI to help guide users past the error.
24 *
25 * To throw an error in a synchronous function you must either pass the error message and actions
26 * directly to the constructor, e.g.
27 *
28 * ```
29 * // To load a message bundle (Note that __dirname should contain a messages folder)
30 * Messages.importMessagesDirectory(__dirname);
31 * const messages = Messages.load('myPackageName', 'myBundleName');
32 *
33 * // To throw a non-bundle based error:
34 * throw new SfError(message.getMessage('myError'), 'MyErrorName');
35 * ```
36 */
37export declare class SfError<T extends ErrorDataProperties = ErrorDataProperties> extends Error {
38 #private;
39 readonly name: string;
40 /**
41 * Action messages. Hints to the users regarding what can be done to fix related issues.
42 */
43 actions?: string[];
44 /**
45 * SfdxCommand can return this process exit code.
46 */
47 exitCode: number;
48 /**
49 * The related context for this error.
50 */
51 context?: string;
52 data?: T;
53 /**
54 * Create an SfError.
55 *
56 * @param message The error message.
57 * @param name The error name. Defaults to 'SfError'.
58 * @param actions The action message(s).
59 * @param exitCodeOrCause The exit code which will be used by SfdxCommand or he underlying error that caused this error to be raised.
60 * @param cause The underlying error that caused this error to be raised.
61 */
62 constructor(message: string, name?: string, actions?: string[], exitCodeOrCause?: number | Error, cause?: unknown);
63 get code(): string;
64 set code(code: string);
65 /** like the constructor, but takes an typed object and let you also set context and data properties */
66 static create<T extends ErrorDataProperties = ErrorDataProperties>(inputs: SfErrorOptions<T>): SfError<T>;
67 /**
68 * Convert an Error to an SfError.
69 *
70 * @param err The error to convert.
71 */
72 static wrap<T extends ErrorDataProperties = ErrorDataProperties>(err: unknown): SfError<T>;
73 /**
74 * Sets the context of the error. For convenience `this` object is returned.
75 *
76 * @param context The command name.
77 */
78 setContext(context: string): SfError;
79 /**
80 * An additional payload for the error. For convenience `this` object is returned.
81 *
82 * @param data The payload data.
83 */
84 setData(data: T): SfError;
85 /**
86 * Convert an {@link SfError} state to an object. Returns a plain object representing the state of this error.
87 */
88 toObject(): SfErrorToObjectResult;
89}
90export {};