http.ErrAbortHandler
GoINFONotableHTTP

net/http: abort Handler

Quick Answer

Panic with http.ErrAbortHandler to silently abort an HTTP handler; the server will close the connection.

What this means

A special sentinel that can be panicked inside an HTTP handler to abort the request without logging a stack trace. It is recovered by the HTTP server.

Why it happens
  1. 1Handler detects an unrecoverable condition and needs to abort without error logging
  2. 2Client disconnected mid-stream and the handler should stop processing

Fix

Panic to abort silently

Panic to abort silently
if clientGone {
    panic(http.ErrAbortHandler)
}

Why this works

The HTTP server recovers this specific panic and closes the connection without logging.

Code examples
Abort on disconnectgo
select {
case <-r.Context().Done():
    panic(http.ErrAbortHandler)
}
Detect in middlewarego
defer func() {
    if v := recover(); v == http.ErrAbortHandler {
        panic(v) // re-panic for server
    }
}()
Context checkgo
if err := r.Context().Err(); err != nil {
    panic(http.ErrAbortHandler)
}
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors