Skip to content

Commit

Permalink
don't re-panic if the io.Writer returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Aug 5, 2024
1 parent f5facc7 commit 8b8957f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ func (c *Config) checkRecover(w io.Writer, r any) {
if r == nil {
return
}
WriteString(w, "<panic>: ")
_, _ = writeString(w, "<panic>: ")
switch r := r.(type) {
case string:
WriteString(w, r)
_, _ = writeString(w, r)
case error:
WriteString(w, r.Error())
_, _ = writeString(w, r.Error())
default:
noErrorWrite(fmt.Fprint(w, r))
_, _ = fmt.Fprint(w, r)
}
WriteString(w, "\n")
_, _ = writeString(w, "\n")
}

func (c *Config) checkValid(w io.Writer, v reflect.Value) bool {
Expand Down
6 changes: 6 additions & 0 deletions pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ func TestConfig(t *testing.T) {
}
}

func TestConfigPanicWriterError(t *testing.T) {
c := newTestConfig()
w := &testErrorWriter{}
c.Write(w, "test")
}

func BenchmarkConfig(b *testing.B) {
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
Expand Down

0 comments on commit 8b8957f

Please sign in to comment.