# Fail

Define a Fail state in the state machine.

Reaching a Fail state terminates the state execution in failure.

Extends: Construct › State › Fail
Implements: IChainable, IConstruct, IDependable

- Source: [`aws-stepfunctions/lib/states/fail.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-stepfunctions/lib/states/fail.ts#L65)
- Type last changed: v2.178.0 · 6 Feb 2025

## Example

```ts
const fail = new sfn.Fail(this, 'Fail', {
  errorPath: sfn.JsonPath.format('error: {}.', sfn.JsonPath.stringAt('$.someError')),
  causePath: "States.Format('cause: {}.', $.someCause)",
});
```

## Initializer

```ts
new Fail(scope: Construct, id: string, props?: FailProps)
```

## Construct Props

- `cause`: string — A description for the cause of the failure.

  **Default:** No description

- `causePath`: string — JsonPath expression to select part of the state to be the cause to this state.

  **Default:** No cause path

  You can also use an intrinsic function that returns a string to specify this property.
  The allowed functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.

- `comment`: string — A comment describing this state.

  **Default:** No comment

- `error`: string — Error code used to represent this failure.

  **Default:** No error code

- `errorPath`: string — JsonPath expression to select part of the state to be the error to this state.

  **Default:** No error path

  You can also use an intrinsic function that returns a string to specify this property.
  The allowed functions include States.Format, States.JsonToString, States.ArrayGetItem, States.Base64Encode, States.Base64Decode, States.Hash, and States.UUID.

- `queryLanguage`: [QueryLanguage](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.QueryLanguage.html) = JSONPath — The name of the query language used by the state.

  If the state does not contain a `queryLanguage` field,
  then it will use the query language specified in the top-level `queryLanguage` field.

- `stateName`: string — Optional name for this state.

  **Default:** The construct ID will be used as state name

## Properties

- `endStates`: [INextable](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.INextable.html)[] — Continuable states of this Chainable.

- `id`: string — Descriptive identifier for this chainable.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `startState`: [State](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.State.html) — First state of this Chainable.

- `stateId`: string — Tokenized string that evaluates to the state's ID.

## Methods

- `addPrefix(x: string): void` — Add a prefix to the stateId of this state.

- `bindToGraph(graph: StateGraph): void` — Register this state as part of the given graph.

  Don't call this. It will be called automatically when you work
  with states normally.

- `filterNextables(states: State[]): INextable[]` (static) — Return only the states that allow chaining from an array of states.

- `findReachableEndStates(start: State, options?: FindStateOptions): State[]` (static) — Find the set of end states states reachable through transitions from the given start state.

- `findReachableStates(start: State, options?: FindStateOptions): State[]` (static) — Find the set of states reachable through transitions from the given start state.

  This does not retrieve states from within sub-graphs, such as states within a Parallel state's branch.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `jsonata(scope: Construct, id: string, props?: FailJsonataProps): Fail // chainable` (static) — Define a Fail state using JSONata in the state machine.

  Reaching a Fail state terminates the state execution in failure.

- `jsonPath(scope: Construct, id: string, props?: FailJsonPathProps): Fail // chainable` (static) — Define a Fail state using JSONPath in the state machine.

  Reaching a Fail state terminates the state execution in failure.

- `prefixStates(root: IConstruct, prefix: string): void` (static) — Add a prefix to the stateId of all States found in a construct tree.

- `toStateJson(queryLanguage?: QueryLanguage): json` — Return the Amazon States Language object for this state.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
