github.com/mongodb/grip@v0.0.0-20240213223901-f906268d82b9/recovery/util.go (about) 1 package recovery 2 3 import ( 4 "errors" 5 "fmt" 6 ) 7 8 func panicString(p interface{}) string { 9 switch panicMesg := p.(type) { 10 case string: 11 return panicMesg 12 case error: 13 return panicMesg.Error() 14 case fmt.Stringer: 15 return panicMesg.String() 16 case nil: 17 return "" 18 default: 19 return fmt.Sprintf("%+v", panicMesg) 20 } 21 } 22 23 func panicError(p interface{}) error { 24 if p == nil { 25 return nil 26 } 27 28 ps := panicString(p) 29 if ps == "" { 30 ps = fmt.Sprintf("non-nil panic [%T] encountered with no string representation", p) 31 } 32 33 return errors.New(ps) 34 }