ErrorToStatusCode extracts gRPC status code from error and returns it. - If err is nil, codes.OK is returned. - If err implements (or wraps error that implements) the method `GRPCStatus() *google.golang.org/grpc/status.Status`, and `GRPCStatus()` returns a non-nil status, code from the status is r
(err error)
| 48 | // |
| 49 | // - Otherwise code.Unknown is returned. |
| 50 | func ErrorToStatusCode(err error) codes.Code { |
| 51 | if err == nil { |
| 52 | return codes.OK |
| 53 | } |
| 54 | type grpcStatus interface{ GRPCStatus() *grpcstatus.Status } |
| 55 | var gs grpcStatus |
| 56 | if errors.As(err, &gs) { |
| 57 | st := gs.GRPCStatus() |
| 58 | if st != nil { |
| 59 | return st.Code() |
| 60 | } |
| 61 | } |
| 62 | return codes.Unknown |
| 63 | } |
| 64 | |
| 65 | // Status creates a new a *github.com/gogo/status.Status with the |
| 66 | // given error code, error message and error details. |