@@ -77,11 +77,18 @@ func FromProto(s *spb.Status) *Status {
77
77
// FromError returns a Status representation of err.
78
78
//
79
79
// - If err was produced by this package or implements the method `GRPCStatus()
80
- // *Status`, or if err wraps a type satisfying this, the appropriate Status is
81
- // returned. For wrapped errors, the message returned contains the entire
82
- // err.Error() text and not just the wrapped status.
80
+ // *Status` and `GRPCStatus()` does not return nil, or if err wraps a type
81
+ // satisfying this, the Status from `GRPCStatus()` is returned. For wrapped
82
+ // errors, the message returned contains the entire err.Error() text and not
83
+ // just the wrapped status. In that case, ok is true.
83
84
//
84
- // - If err is nil, a Status is returned with codes.OK and no message.
85
+ // - If err is nil, a Status is returned with codes.OK and no message, and ok
86
+ // is true.
87
+ //
88
+ // - If err implements the method `GRPCStatus() *Status` and `GRPCStatus()`
89
+ // returns nil (which maps to Codes.OK), or if err wraps a type
90
+ // satisfying this, a Status is returned with codes.Unknown and err's
91
+ // Error() message, and ok is false.
85
92
//
86
93
// - Otherwise, err is an error not compatible with this package. In this
87
94
// case, a Status is returned with codes.Unknown and err's Error() message,
@@ -92,10 +99,24 @@ func FromError(err error) (s *Status, ok bool) {
92
99
}
93
100
type grpcstatus interface { GRPCStatus () * Status }
94
101
if gs , ok := err .(grpcstatus ); ok {
102
+ if gs .GRPCStatus () == nil {
103
+ // Error has status nil, which maps to codes.OK. There
104
+ // is no sensible behavior for this, so we turn it into
105
+ // an error with codes.Unknown and discard the existing
106
+ // status.
107
+ return New (codes .Unknown , err .Error ()), false
108
+ }
95
109
return gs .GRPCStatus (), true
96
110
}
97
111
var gs grpcstatus
98
112
if errors .As (err , & gs ) {
113
+ if gs .GRPCStatus () == nil {
114
+ // Error wraps an error that has status nil, which maps
115
+ // to codes.OK. There is no sensible behavior for this,
116
+ // so we turn it into an error with codes.Unknown and
117
+ // discard the existing status.
118
+ return New (codes .Unknown , err .Error ()), false
119
+ }
99
120
p := gs .GRPCStatus ().Proto ()
100
121
p .Message = err .Error ()
101
122
return status .FromProto (p ), true
0 commit comments