MCPcopy
hub / github.com/grafana/dskit / ErrorToStatusCode

Function ErrorToStatusCode

grpcutil/status.go:50–63  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

48//
49// - Otherwise code.Unknown is returned.
50func 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.

Callers 3

errorToStatusCodeFunction · 0.92
IsCanceledFunction · 0.85
TestErrorToStatusCodeFunction · 0.85

Calls 1

GRPCStatusMethod · 0.95

Tested by 1

TestErrorToStatusCodeFunction · 0.68