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)
| 94 | // case, a Status is returned with codes.Unknown and err's Error() message, |
| 95 | // and ok is false. |
| 96 | func 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. |