| 1 | --- |
| 2 | title: "htmx:response:error" |
| 3 | description: "When an HTTP error status (4xx/5xx) is received" |
| 4 | --- |
| 5 | |
| 6 | The `htmx:response:error` event fires when the server responds with an HTTP error status code (400 or higher). |
| 7 | |
| 8 | ## When It Fires |
| 9 | |
| 10 | After [`htmx:after:request`](/reference/events/htmx-after-request) and before the swap phase, when `response.status >= 400`. |
| 11 | |
| 12 | This event does **not** fire for network errors or timeouts — use [`htmx:error`](/reference/events/htmx-error) for those. |
| 13 | |
| 14 | ## Event Detail |
| 15 | |
| 16 | - `ctx` - The full request context, including: |
| 17 | - `ctx.response.status` - The HTTP status code |
| 18 | - `ctx.response.headers` - Response headers |
| 19 | - `ctx.text` - The response body |
| 20 | |
| 21 | ## Example |
| 22 | |
| 23 | ```javascript |
| 24 | htmx.on('htmx:response:error', (evt) => { |
| 25 | let status = evt.detail.ctx.response.status; |
| 26 | console.error(`HTTP ${status} error`); |
| 27 | }); |
| 28 | ``` |
| 29 | |
| 30 | ## Migration from htmx 2 |
| 31 | |
| 32 | This event replaces htmx 2's `htmx:responseError`. The event detail structure has changed — the response |
| 33 | information is now available via `evt.detail.ctx.response` rather than directly on `evt.detail`. |