Skip to content

Commit e8fe6ad

Browse files
committed
attributes: use fmt.Sprint(v) instead of v.String() and recover, let Go handle the panic itself.
https://github.com/grpc/grpc-go/pull/6574/files/5936668fb03365179b2900bb139865add6b4aa93#r1324886324
1 parent 5936668 commit e8fe6ad

File tree

1 file changed

+1
-17
lines changed

1 file changed

+1
-17
lines changed

attributes/attributes.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,10 @@ func (a *Attributes) String() string {
125125
const nilAngleString = "<nil>"
126126

127127
func str(x any) (s string) {
128-
defer func() {
129-
if r := recover(); r != nil {
130-
// If it panics with a nil pointer, just say "<nil>". The likeliest causes are a
131-
// [fmt.Stringer] that fails to guard against nil or a nil pointer for a
132-
// value receiver, and in either case, "<nil>" is a nice result.
133-
//
134-
// Adapted from the code in fmt/print.go.
135-
if v := reflect.ValueOf(x); v.Kind() == reflect.Pointer && v.IsNil() {
136-
s = nilAngleString
137-
return
138-
}
139-
140-
// The panic was likely not caused by fmt.Stringer.
141-
panic(r)
142-
}
143-
}()
144128
if x == nil { // NOTE: typed nils will not be caught by this check
145129
return nilAngleString
146130
} else if v, ok := x.(fmt.Stringer); ok {
147-
return v.String()
131+
return fmt.Sprint(v)
148132
} else if v, ok := x.(string); ok {
149133
return v
150134
}

0 commit comments

Comments
 (0)