# Handle formatting errors

> Control how Smart Strings react to invalid format strings and unformattable values, and track the failures that occur.

Control how Smart Strings react to invalid format strings and unformattable values, and track the failures that occur.

Smart Strings can fail at two stages: when the [Parser](/engine/6000.7/manual/scripting/smart-strings.md#how-a-smart-string-is-formatted) reads the format string, and when the [SmartFormatter](/engine/6000.7/manual/scripting/smart-strings.md) evaluates a placeholder. Each stage has its own error action that decides whether a failure raises an exception, appears in the output, is skipped, or leaves the placeholder unchanged.

## Error actions

The parser error action is [`ParserSettings.ErrorAction`](/engine/6000.7/script-reference/unity/smartstrings/core/settings/parsersettings/erroraction.md), of type [`ParseErrorAction`](/engine/6000.7/script-reference/unity/smartstrings/core/settings/parseerroraction.md).

The formatter error action is [`FormatterSettings.ErrorAction`](/engine/6000.7/script-reference/unity/smartstrings/core/settings/formattersettings/erroraction.md), of type [`FormatErrorAction`](/engine/6000.7/script-reference/unity/smartstrings/core/settings/formaterroraction.md).

Both enums define the same four values in the same order, and both default to `ThrowError`.

| Value                 | What it does                                                                                           |
| --------------------- | ------------------------------------------------------------------------------------------------------ |
| `ThrowError`          | Raises an exception. This is the default. Use this while debugging so that errors surface immediately. |
| `OutputErrorInResult` | Writes the error message into the formatted result instead of throwing.                                |
| `Ignore`              | Skips the error and continues, producing no output for the failed item.                                |
| `MaintainTokens`      | Leaves the offending token unchanged in the output, so the original placeholder text is preserved.     |

## Set the error actions

Set the error actions in the editor or in code:

* In the editor, open **Edit > Project Settings > Smart Strings** and use the **Error Action** dropdown under the **Parser** and **Formatter** sections. Refer to [Smart Strings settings reference](/engine/6000.7/manual/scripting/smart-strings/settings.md).
* In code, set them on the active formatter's settings:

```cs
Smart.Default.Settings.Parser.ErrorAction = ParseErrorAction.OutputErrorInResult;
            Smart.Default.Settings.Formatter.ErrorAction = FormatErrorAction.Ignore;
```

> **Note:**
>
> Settings are intended to be applied during initialization. Change the error actions before the formatter and parser process any Smart String.

## Track failures

To observe failures without changing the error action, subscribe to the failure events. Both events fire whether or not an exception is thrown.

To track failures in the formatter, subscribe to [`SmartFormatter.OnFormattingFailure`](/engine/6000.7/script-reference/unity/smartstrings/smartformatter/onformattingfailure.md) events. This event raises a [`FormattingErrorEventArgs`](/engine/6000.7/script-reference/unity/smartstrings/formattingerroreventargs.md) that reports the [`Placeholder`](/engine/6000.7/script-reference/unity/smartstrings/core/parsing/placeholder.md) text, the `ErrorIndex` within the format string, and `IgnoreError`, which is `true` when the error action is anything other than `ThrowError`.

To track failures in the parser, subscribe to [`Parser.OnParsingFailure`](/engine/6000.7/script-reference/unity/smartstrings/core/parsing/parser/onparsingfailure.md) events. This event raises a [`ParsingErrorEventArgs`](/engine/6000.7/script-reference/unity/smartstrings/core/parsing/parsingerroreventargs.md) that reports the parsing `Errors` and `ThrowsException`, which is `true` when the parser error action is `ThrowError`. Reach the parser through `Smart.Default.Parser`.

```cs
Smart.Default.OnFormattingFailure += (sender, e) =>
                Debug.LogWarning($"Smart String failed at {e.ErrorIndex}: {e.Placeholder}");
```

When the error action is `ThrowError`, a formatting failure throws a [`FormattingException`](/engine/6000.7/script-reference/unity/smartstrings/core/formatting/formattingexception.md). It exposes:

| Member                                                                                | Description                                                                                                              |
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `Issue`                                                                               | A description of the error.                                                                                              |
| [`Format`](/engine/6000.7/script-reference/unity/smartstrings/core/parsing/format.md) | The base format string that caused the error.                                                                            |
| `Index`                                                                               | The index inside the format string where the error occurred.                                                             |
| `ErrorItem`                                                                           | The [`FormatItem`](/engine/6000.7/script-reference/unity/smartstrings/core/parsing/formatitem.md) that caused the error. |

## Additional resources

* [Smart Strings settings reference](/engine/6000.7/manual/scripting/smart-strings/settings.md)
* [Smart Strings performance](/engine/6000.7/manual/scripting/smart-strings/performance.md)
* [Smart Strings](/engine/6000.7/manual/scripting/smart-strings.md)
