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');

Type Parameters

  • T extends ErrorDataProperties = ErrorDataProperties

Hierarchy

  • Error
    • SfError

Constructors

  • Create an SfError.

    Type Parameters

    • T extends AnyJson = AnyJson

    Parameters

    • message: string

      The error message.

    • name: string = 'SfError'

      The error name. Defaults to 'SfError'.

    • Optional actions: string[]

      The action message(s).

    • Optional exitCodeOrCause: number | Error

      The exit code which will be used by SfdxCommand or he underlying error that caused this error to be raised.

    • Optional cause: unknown

      The underlying error that caused this error to be raised.

    Returns SfError<T>

Properties

actions?: string[]

Action messages. Hints to the users regarding what can be done to fix related issues.

context?: string

The related context for this error.

data?: T
exitCode: number

SfdxCommand can return this process exit code.

name: string

Accessors

Methods

  • Sets the context of the error. For convenience this object is returned.

    Parameters

    • context: string

      The command name.

    Returns SfError<AnyJson>

  • An additional payload for the error. For convenience this object is returned.

    Parameters

    • data: T

      The payload data.

    Returns SfError<AnyJson>

  • Convert an SfError state to an object. Returns a plain object representing the state of this error.

    Returns SfErrorToObjectResult

  • like the constructor, but takes an typed object and let you also set context and data properties

    Type Parameters

    • T extends AnyJson = AnyJson

    Parameters

    Returns SfError<T>

  • Convert an Error to an SfError.

    Type Parameters

    • T extends AnyJson = AnyJson

    Parameters

    • err: unknown

      The error to convert.

    Returns SfError<T>