| 1 | --- |
| 2 | title: "htmx:error" |
| 3 | description: "When an error occurs during request" |
| 4 | --- |
| 5 | |
| 6 | The `htmx:error` event fires when an exception occurs during the request or swap process. |
| 7 | |
| 8 | ## When It Fires |
| 9 | |
| 10 | - Network errors (connection failed, timeout) |
| 11 | - Swap errors (invalid content, DOM errors) |
| 12 | - Any other exception during the htmx request lifecycle |
| 13 | |
| 14 | This event does **not** fire for HTTP error statuses (4xx/5xx) — use [`htmx:response:error`](/reference/events/htmx-response-error) for those. |
| 15 | |
| 16 | ## Event Detail |
| 17 | |
| 18 | - `ctx` - Request context at time of error |
| 19 | - `error` - The exception object |
| 20 | |
| 21 | ## Example |
| 22 | |
| 23 | ```javascript |
| 24 | htmx.on('htmx:error', (evt) => { |
| 25 | console.error('Request failed:', evt.detail.error); |
| 26 | console.log('Context:', evt.detail.ctx); |
| 27 | // Show error message to user |
| 28 | }); |
| 29 | ``` |
| 30 | |
| 31 | Use this for centralized error handling and user feedback. |