Skip to content

Commit 7fcb8a2

Browse files
VojtechVitekalliasgherclaude
authored
middleware: document printPrettyStack and harden NoColor panic test (#1131)
* middleware: honour NoColor in defaultLogEntry.Panic defaultLogEntry.Panic called PrintPrettyStack unconditionally, which always used ANSI colour codes regardless of the DefaultLogFormatter's NoColor field. Add PrintPrettyStackColor(rvr, useColor bool) and thread the flag through prettyStack.parse. PrintPrettyStack now delegates to it with useColor=true so existing callers are unaffected. defaultLogEntry.Panic passes l.useColor so the panic output respects the same flag that controls the rest of the request log. Fixes #1042 Signed-off-by: alliasgher <alliasgher123@gmail.com> * middleware: keep color-aware stack printer unexported Address review: only defaultLogEntry.Panic needs the useColor variant and it lives in the same package, so PrintPrettyStackColor does not need to be exported. Rename it to printPrettyStack; the public PrintPrettyStack API is unchanged. Signed-off-by: alliasgher <alliasgher123@gmail.com> * middleware: harden TestRecovererNoColor to actually guard the regression Force IsTTY=true so cW would emit ANSI codes if useColor were true. Without it the assertion is a no-op on non-TTY runners (e.g. CI) and passes even against the buggy code path. Supersedes #1061. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Signed-off-by: alliasgher <alliasgher123@gmail.com> Co-authored-by: alliasgher <alliasgher123@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 878fe71 commit 7fcb8a2

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎middleware/recoverer.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func PrintPrettyStack(rvr interface{}) {
5555
printPrettyStack(rvr, true)
5656
}
5757

58+
// printPrettyStack prints a formatted stack trace to stderr. When useColor is
59+
// false, ANSI colour codes are suppressed, which is useful for terminals that
60+
// do not support them (e.g. on Windows) or when output is being captured.
5861
func printPrettyStack(rvr interface{}, useColor bool) {
5962
debugStack := debug.Stack()
6063
s := prettyStack{}

‎middleware/recoverer_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func TestRecoverer(t *testing.T) {
4646
}
4747

4848
func TestRecovererNoColor(t *testing.T) {
49+
// Force IsTTY so cW would emit color codes if useColor were true; this is
50+
// what makes the assertion below a real regression guard rather than a
51+
// no-op on non-TTY test runners (e.g. CI).
52+
oldIsTTY := IsTTY
53+
IsTTY = true
54+
defer func() { IsTTY = oldIsTTY }()
55+
4956
oldRecovererErrorWriter := recovererErrorWriter
5057
defer func() { recovererErrorWriter = oldRecovererErrorWriter }()
5158
buf := &bytes.Buffer{}

0 commit comments

Comments
 (0)