Skip to content

Commit 21354b1

Browse files
committed
log request during panic when in debug mode
1 parent 48c3482 commit 21354b1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

recovery.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io/ioutil"
1212
"log"
1313
"net/http"
14+
"net/http/httputil"
1415
"runtime"
1516
"time"
1617
)
@@ -38,7 +39,12 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc {
3839
if err := recover(); err != nil {
3940
if logger != nil {
4041
stack := stack(3)
41-
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", timeFormat(time.Now()), err, stack, reset)
42+
if IsDebugging() {
43+
httprequest, _ := httputil.DumpRequest(c.Request, false)
44+
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", timeFormat(time.Now()), string(httprequest), err, stack, reset)
45+
} else {
46+
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", timeFormat(time.Now()), err, stack, reset)
47+
}
4248
}
4349
c.AbortWithStatus(http.StatusInternalServerError)
4450
}

recovery_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ func TestPanicInHandler(t *testing.T) {
2727
assert.Contains(t, buffer.String(), "panic recovered")
2828
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
2929
assert.Contains(t, buffer.String(), "TestPanicInHandler")
30+
31+
// Debug mode prints the request
32+
SetMode(DebugMode)
33+
// RUN
34+
w = performRequest(router, "GET", "/recovery")
35+
// TEST
36+
assert.Equal(t, http.StatusInternalServerError, w.Code)
37+
assert.Contains(t, buffer.String(), "GET /recovery")
38+
3039
}
3140

3241
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.

0 commit comments

Comments
 (0)