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

issues/162: Panic middleware should include correct stack trace. #164

Merged
merged 2 commits into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Most recent version is listed first.


## v0.0.22
- Panic middleware should include correct stack trace: https://github.com/komuw/ong/pull/164

## v0.0.21
- Improve performance of calling Csrf middleware multiple times: https://github.com/komuw/ong/pull/161

Expand Down
3 changes: 2 additions & 1 deletion middleware/panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"

"github.com/komuw/ong/errors"
"github.com/komuw/ong/log"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func Panic(wrappedHandler http.HandlerFunc, l log.Logger) http.HandlerFunc {
w.Header().Del(ongMiddlewareErrorHeader) // remove header so that users dont see it.

if e, ok := errR.(error); ok {
reqL.Error(e, flds)
reqL.Error(errors.Wrap(e), flds) // wrap with ong/errors so that the log will have a stacktrace.
} else {
reqL.Error(nil, flds)
}
Expand Down
27 changes: 27 additions & 0 deletions middleware/panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func handlerThatPanics(msg string, shouldPanic bool, err error) http.HandlerFunc
}
}

func anotherHandlerThatPanics() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_ = 90
someSlice := []string{"zero", "one", "two"}
_ = "kilo"
_ = someSlice[16] // panic

fmt.Fprint(w, "anotherHandlerThatPanics")
}
}

func TestPanic(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -115,6 +126,22 @@ func TestPanic(t *testing.T) {
attest.Subsequence(t, logOutput.String(), "stack")
})

t.Run("stacktrace has correct line", func(t *testing.T) {
t.Parallel()

logOutput := &bytes.Buffer{}
wrappedHandler := Panic(anotherHandlerThatPanics(), getLogger(logOutput))

rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/someUri", nil)
wrappedHandler.ServeHTTP(rec, req)

res := rec.Result()
defer res.Body.Close()
attest.Equal(t, res.StatusCode, http.StatusInternalServerError)
attest.Subsequence(t, logOutput.String(), "middleware/panic_test.go:40") // line where panic happened.
})

t.Run("concurrency safe", func(t *testing.T) {
t.Parallel()

Expand Down