Skip to content

Commit

Permalink
Move panic handling to testing
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Mar 1, 2024
1 parent 235a2df commit 95b2956
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
12 changes: 0 additions & 12 deletions transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,6 @@ func (o *operation) queryValues() url.Values {
}

func (o *operation) handle() {
// Deferred function to capture http.ErrAbortHandler panics.
defer func() {
if recovered := recover(); recovered != nil {
if err, ok := recovered.(error); ok {
if errors.Is(err, http.ErrAbortHandler) {
return
}
}
panic(recovered) //nolint:forbidigo // re-throw the panic if unknown
}
}()

o.clientEnveloper, _ = o.client.protocol.(envelopedProtocolHandler)
o.clientPreparer, _ = o.client.protocol.(clientBodyPreparer)
if o.clientPreparer != nil {
Expand Down
24 changes: 20 additions & 4 deletions vanguard_restxrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -168,9 +169,6 @@ func TestMux_RESTxRPC(t *testing.T) {
query[key] = values
}
req.URL.RawQuery = query.Encode()
// Inject http.Server into the request context for httputil.ReverseProxy.
svr := &http.Server{} //nolint:gosec // dummy server for testing
req = req.WithContext(context.WithValue(req.Context(), http.ServerContextKey, svr))
return req
}
type output struct {
Expand Down Expand Up @@ -552,7 +550,25 @@ func TestMux_RESTxRPC(t *testing.T) {
t.Log("req:", string(debug))

rsp := httptest.NewRecorder()
opts.mux.handler.ServeHTTP(rsp, req)

func() {
// Capture http.ErrAbortHanlder panics.
defer func() {
if recovered := recover(); recovered != nil {
if err, ok := recovered.(error); ok {
if errors.Is(err, http.ErrAbortHandler) {
return
}
}
t.Error("unexpected panic:", recovered)
}
}()
// Inject http.Server into the request context to convince
// httputil.ReverseProxy we are in a server context.
svr := &http.Server{} //nolint:gosec // dummy server for testing
req = req.WithContext(context.WithValue(req.Context(), http.ServerContextKey, svr))
opts.mux.handler.ServeHTTP(rsp, req)
}()

result := rsp.Result()
defer result.Body.Close()
Expand Down

0 comments on commit 95b2956

Please sign in to comment.