> For the complete documentation index, see [llms.txt](https://sevenval.gitbook.io/flat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sevenval.gitbook.io/flat/develop/cookbook/error-flow.md).

# Handling Errors with an Error Flow

The [*error flow*](/flat/develop/reference/openapi-4/routing.md#error-flow) is triggered when certain severe errors occur while processing the flow, or when the [`error` action](/flat/develop/reference/actions/error.md) is invoked. It is typically used to produce custom error messages or headers, or set the HTTP status. Another example is augmenting the [access log](/flat/develop/administration/logging.md) with [custom log fields](/flat/develop/cookbook/custom-logging.md).

In your `swagger.yaml`, point the top-level property `x-flat-error` to a flow file:

```yaml
…
x-flat-error:
  flow: error.xml       # ⬅ error flow
…
```

In this example, we use `error.xml` to format a custom error message:

```markup
<flow>
  <template>
    {
      "CustomError":  {
        "Message": {{ $error/message }},
        "Info": {{ $error/info }}
      }
    }
  </template>
  <set-response-headers>
    {
      "Status": {{ $error/status }},
      "Error-Code": {{ $error/code }}
    }
  </set-response-headers>
</flow>
```

This will ensure that all errors that trigger the error flow will produce a consistent status, error message and headers.

## Triggers

Error triggers are

* [schema violations](/flat/develop/reference/openapi-7/validation.md),
* [security violations (JWT)](/flat/develop/reference/openapi-5/security.md),
* [request errors](/flat/develop/reference/actions/request.md#options),
* or the [`error` action](/flat/develop/reference/actions/error.md)

## See also

* [Error Flow](/flat/develop/reference/openapi-4/routing.md#error-flow)
* [`$error`](/flat/develop/reference/variables.md#usderror)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sevenval.gitbook.io/flat/develop/cookbook/error-flow.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
