Skip to content

feat: add panic recovery middleware #3687

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

Merged
merged 7 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
unexport ResponseBody
  • Loading branch information
sreya committed Aug 29, 2022
commit 1086ff905ec46a083a791a423749b8a60dac86c5
10 changes: 7 additions & 3 deletions coderd/httpapi/status_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type StatusWriter struct {
http.ResponseWriter
Status int
Hijacked bool
ResponseBody []byte
responseBody []byte

wroteHeader bool
}
Expand All @@ -45,8 +45,8 @@ func (w *StatusWriter) Write(b []byte) (int, error) {
// we typically only write to the response body once
// and this field is only used for logging I'm leaving
// this as-is.
w.ResponseBody = make([]byte, minInt(len(b), maxBodySize))
copy(w.ResponseBody, b)
w.responseBody = make([]byte, minInt(len(b), maxBodySize))
copy(w.responseBody, b)
}

return w.ResponseWriter.Write(b)
Expand All @@ -68,3 +68,7 @@ func (w *StatusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {

return hijacker.Hijack()
}

func (w *StatusWriter) ResponseBody() []byte {
return w.responseBody
}
6 changes: 3 additions & 3 deletions coderd/httpapi/status_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestStatusWriter(t *testing.T) {
// Should set the status to OK.
require.Equal(t, http.StatusOK, w.Status)
// We don't record the body for codes <400.
require.Equal(t, []byte(nil), w.ResponseBody)
require.Equal(t, []byte(nil), w.ResponseBody())
require.Equal(t, body, rec.Body.Bytes())
})

Expand All @@ -80,7 +80,7 @@ func TestStatusWriter(t *testing.T) {
require.NoError(t, err)

require.Equal(t, code, w.Status)
require.Equal(t, body, w.ResponseBody)
require.Equal(t, body, w.ResponseBody())
require.Equal(t, body, rec.Body.Bytes())
})

Expand All @@ -103,7 +103,7 @@ func TestStatusWriter(t *testing.T) {

require.Equal(t, code, w.Status)
require.Equal(t, body, rec.Body.Bytes())
require.Equal(t, body[:4096], w.ResponseBody)
require.Equal(t, body[:4096], w.ResponseBody())
})

t.Run("Hijack", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/httpmw/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
// want to log the response body.
if sw.Status >= 400 {
httplog = httplog.With(
slog.F("response_body", string(sw.ResponseBody)),
slog.F("response_body", string(sw.ResponseBody())),
)
}

Expand Down