Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recoverer: don't recover http.ErrAbortHandler #624

Merged
merged 1 commit into from Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion middleware/recoverer.go
Expand Up @@ -22,7 +22,12 @@ import (
func Recoverer(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler {
if rvr := recover(); rvr != nil {
if rvr == http.ErrAbortHandler {
drakkan marked this conversation as resolved.
Show resolved Hide resolved
// we don't recover http.ErrAbortHandler so the response
// to the client is aborted, this should not be logged
panic(rvr)
}

logEntry := GetLogEntry(r)
if logEntry != nil {
Expand Down
25 changes: 25 additions & 0 deletions middleware/recoverer_test.go
Expand Up @@ -40,3 +40,28 @@ func TestRecoverer(t *testing.T) {
}
t.Fatal("First func call line should start with ->.")
}

func TestRecovererAbortHandler(t *testing.T) {
defer func() {
rcv := recover()
if rcv != http.ErrAbortHandler {
t.Fatalf("http.ErrAbortHandler should not be recovered")
}
}()

w := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(Recoverer)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic(http.ErrAbortHandler)
})

req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

r.ServeHTTP(w, req)
}