MCPcopy
hub / github.com/grpc/grpc-go / FromError

Function FromError

status/status.go:96–127  ·  view source on GitHub ↗

FromError returns a Status representation of err. - If err was produced by this package or implements the method `GRPCStatus() *Status` and `GRPCStatus()` does not return nil, or if err wraps a type satisfying this, the Status from `GRPCStatus()` is returned. For wrapped errors, the message return

(err error)

Source from the content-addressed store, hash-verified

94// case, a Status is returned with codes.Unknown and err's Error() message,
95// and ok is false.
96func FromError(err error) (s *Status, ok bool) {
97 if err == nil {
98 return nil, true
99 }
100 type grpcstatus interface{ GRPCStatus() *Status }
101 if gs, ok := err.(grpcstatus); ok {
102 grpcStatus := gs.GRPCStatus()
103 if grpcStatus == nil {
104 // Error has status nil, which maps to codes.OK. There
105 // is no sensible behavior for this, so we turn it into
106 // an error with codes.Unknown and discard the existing
107 // status.
108 return New(codes.Unknown, err.Error()), false
109 }
110 return grpcStatus, true
111 }
112 var gs grpcstatus
113 if errors.As(err, &gs) {
114 grpcStatus := gs.GRPCStatus()
115 if grpcStatus == nil {
116 // Error wraps an error that has status nil, which maps
117 // to codes.OK. There is no sensible behavior for this,
118 // so we turn it into an error with codes.Unknown and
119 // discard the existing status.
120 return New(codes.Unknown, err.Error()), false
121 }
122 p := grpcStatus.Proto()
123 p.Message = err.Error()
124 return status.FromProto(p), true
125 }
126 return New(codes.Unknown, err.Error()), false
127}
128
129// Convert is a convenience function which removes the need to handle the
130// boolean return value from FromError.

Callers 15

processUnaryRPCMethod · 0.92
processStreamingRPCMethod · 0.92
errorDescFunction · 0.92
newClientStreamFunction · 0.92
SendMsgMethod · 0.92
RecvMsgMethod · 0.92
toRPCErrFunction · 0.92
pickMethod · 0.92
TestToRPCErrMethod · 0.92
newServerTrailerEntryMethod · 0.92
errToPickResultFunction · 0.92

Calls 5

GRPCStatusMethod · 0.95
FromProtoFunction · 0.92
ProtoMethod · 0.80
NewFunction · 0.70
ErrorMethod · 0.65