aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/grpc/status/status.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/grpc/status/status.go')
-rw-r--r--vendor/google.golang.org/grpc/status/status.go52
1 files changed, 47 insertions, 5 deletions
diff --git a/vendor/google.golang.org/grpc/status/status.go b/vendor/google.golang.org/grpc/status/status.go
index 871dc4b..ed36681 100644
--- a/vendor/google.golang.org/grpc/status/status.go
+++ b/vendor/google.golang.org/grpc/status/status.go
@@ -28,6 +28,7 @@
28package status 28package status
29 29
30import ( 30import (
31 "context"
31 "errors" 32 "errors"
32 "fmt" 33 "fmt"
33 34
@@ -46,7 +47,7 @@ func (se *statusError) Error() string {
46 return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage()) 47 return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage())
47} 48}
48 49
49func (se *statusError) status() *Status { 50func (se *statusError) GRPCStatus() *Status {
50 return &Status{s: (*spb.Status)(se)} 51 return &Status{s: (*spb.Status)(se)}
51} 52}
52 53
@@ -120,15 +121,25 @@ func FromProto(s *spb.Status) *Status {
120} 121}
121 122
122// FromError returns a Status representing err if it was produced from this 123// FromError returns a Status representing err if it was produced from this
123// package, otherwise it returns nil, false. 124// package or has a method `GRPCStatus() *Status`. Otherwise, ok is false and a
125// Status is returned with codes.Unknown and the original error message.
124func FromError(err error) (s *Status, ok bool) { 126func FromError(err error) (s *Status, ok bool) {
125 if err == nil { 127 if err == nil {
126 return &Status{s: &spb.Status{Code: int32(codes.OK)}}, true 128 return &Status{s: &spb.Status{Code: int32(codes.OK)}}, true
127 } 129 }
128 if s, ok := err.(*statusError); ok { 130 if se, ok := err.(interface {
129 return s.status(), true 131 GRPCStatus() *Status
132 }); ok {
133 return se.GRPCStatus(), true
130 } 134 }
131 return nil, false 135 return New(codes.Unknown, err.Error()), false
136}
137
138// Convert is a convenience function which removes the need to handle the
139// boolean return value from FromError.
140func Convert(err error) *Status {
141 s, _ := FromError(err)
142 return s
132} 143}
133 144
134// WithDetails returns a new status with the provided details messages appended to the status. 145// WithDetails returns a new status with the provided details messages appended to the status.
@@ -166,3 +177,34 @@ func (s *Status) Details() []interface{} {
166 } 177 }
167 return details 178 return details
168} 179}
180
181// Code returns the Code of the error if it is a Status error, codes.OK if err
182// is nil, or codes.Unknown otherwise.
183func Code(err error) codes.Code {
184 // Don't use FromError to avoid allocation of OK status.
185 if err == nil {
186 return codes.OK
187 }
188 if se, ok := err.(interface {
189 GRPCStatus() *Status
190 }); ok {
191 return se.GRPCStatus().Code()
192 }
193 return codes.Unknown
194}
195
196// FromContextError converts a context error into a Status. It returns a
197// Status with codes.OK if err is nil, or a Status with codes.Unknown if err is
198// non-nil and not a context error.
199func FromContextError(err error) *Status {
200 switch err {
201 case nil:
202 return New(codes.OK, "")
203 case context.DeadlineExceeded:
204 return New(codes.DeadlineExceeded, err.Error())
205 case context.Canceled:
206 return New(codes.Canceled, err.Error())
207 default:
208 return New(codes.Unknown, err.Error())
209 }
210}