Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 gRPC authors. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | // Package status implements errors returned by gRPC. These errors are |
| 20 | // serialized and transmitted on the wire between server and client, and allow |
| 21 | // for additional data to be transmitted via the Details field in the status |
| 22 | // proto. gRPC service handlers should return an error created by this |
| 23 | // package, and gRPC clients should expect a corresponding error to be |
| 24 | // returned from the RPC call. |
| 25 | // |
| 26 | // This package upholds the invariants that a non-nil error may not |
| 27 | // contain an OK code, and an OK code must result in a nil error. |
| 28 | package status |
| 29 | |
| 30 | import ( |
| 31 | "context" |
| 32 | "errors" |
| 33 | "fmt" |
| 34 | |
| 35 | "github.com/golang/protobuf/proto" |
| 36 | "github.com/golang/protobuf/ptypes" |
| 37 | spb "google.golang.org/genproto/googleapis/rpc/status" |
| 38 | "google.golang.org/grpc/codes" |
| 39 | "google.golang.org/grpc/internal" |
| 40 | ) |
| 41 | |
| 42 | func init() { |
| 43 | internal.StatusRawProto = statusRawProto |
| 44 | } |
| 45 | |
| 46 | func statusRawProto(s *Status) *spb.Status { return s.s } |
| 47 | |
| 48 | // statusError is an alias of a status proto. It implements error and Status, |
| 49 | // and a nil statusError should never be returned by this package. |
| 50 | type statusError spb.Status |
| 51 | |
| 52 | func (se *statusError) Error() string { |
| 53 | p := (*spb.Status)(se) |
| 54 | return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage()) |
| 55 | } |
| 56 | |
| 57 | func (se *statusError) GRPCStatus() *Status { |
| 58 | return &Status{s: (*spb.Status)(se)} |
| 59 | } |
| 60 | |
| 61 | // Is implements future error.Is functionality. |
| 62 | // A statusError is equivalent if the code and message are identical. |
| 63 | func (se *statusError) Is(target error) bool { |
| 64 | tse, ok := target.(*statusError) |
| 65 | if !ok { |
| 66 | return false |
| 67 | } |
| 68 | |
| 69 | return proto.Equal((*spb.Status)(se), (*spb.Status)(tse)) |
| 70 | } |
| 71 | |
| 72 | // Status represents an RPC status code, message, and details. It is immutable |
| 73 | // and should be created with New, Newf, or FromProto. |
| 74 | type Status struct { |
| 75 | s *spb.Status |
| 76 | } |
| 77 | |
| 78 | // Code returns the status code contained in s. |
| 79 | func (s *Status) Code() codes.Code { |
| 80 | if s == nil || s.s == nil { |
| 81 | return codes.OK |
| 82 | } |
| 83 | return codes.Code(s.s.Code) |
| 84 | } |
| 85 | |
| 86 | // Message returns the message contained in s. |
| 87 | func (s *Status) Message() string { |
| 88 | if s == nil || s.s == nil { |
| 89 | return "" |
| 90 | } |
| 91 | return s.s.Message |
| 92 | } |
| 93 | |
| 94 | // Proto returns s's status as an spb.Status proto message. |
| 95 | func (s *Status) Proto() *spb.Status { |
| 96 | if s == nil { |
| 97 | return nil |
| 98 | } |
| 99 | return proto.Clone(s.s).(*spb.Status) |
| 100 | } |
| 101 | |
| 102 | // Err returns an immutable error representing s; returns nil if s.Code() is |
| 103 | // OK. |
| 104 | func (s *Status) Err() error { |
| 105 | if s.Code() == codes.OK { |
| 106 | return nil |
| 107 | } |
| 108 | return (*statusError)(s.s) |
| 109 | } |
| 110 | |
| 111 | // New returns a Status representing c and msg. |
| 112 | func New(c codes.Code, msg string) *Status { |
| 113 | return &Status{s: &spb.Status{Code: int32(c), Message: msg}} |
| 114 | } |
| 115 | |
| 116 | // Newf returns New(c, fmt.Sprintf(format, a...)). |
| 117 | func Newf(c codes.Code, format string, a ...interface{}) *Status { |
| 118 | return New(c, fmt.Sprintf(format, a...)) |
| 119 | } |
| 120 | |
| 121 | // Error returns an error representing c and msg. If c is OK, returns nil. |
| 122 | func Error(c codes.Code, msg string) error { |
| 123 | return New(c, msg).Err() |
| 124 | } |
| 125 | |
| 126 | // Errorf returns Error(c, fmt.Sprintf(format, a...)). |
| 127 | func Errorf(c codes.Code, format string, a ...interface{}) error { |
| 128 | return Error(c, fmt.Sprintf(format, a...)) |
| 129 | } |
| 130 | |
| 131 | // ErrorProto returns an error representing s. If s.Code is OK, returns nil. |
| 132 | func ErrorProto(s *spb.Status) error { |
| 133 | return FromProto(s).Err() |
| 134 | } |
| 135 | |
| 136 | // FromProto returns a Status representing s. |
| 137 | func FromProto(s *spb.Status) *Status { |
| 138 | return &Status{s: proto.Clone(s).(*spb.Status)} |
| 139 | } |
| 140 | |
| 141 | // FromError returns a Status representing err if it was produced from this |
| 142 | // package or has a method `GRPCStatus() *Status`. Otherwise, ok is false and a |
| 143 | // Status is returned with codes.Unknown and the original error message. |
| 144 | func FromError(err error) (s *Status, ok bool) { |
| 145 | if err == nil { |
| 146 | return nil, true |
| 147 | } |
| 148 | if se, ok := err.(interface { |
| 149 | GRPCStatus() *Status |
| 150 | }); ok { |
| 151 | return se.GRPCStatus(), true |
| 152 | } |
| 153 | return New(codes.Unknown, err.Error()), false |
| 154 | } |
| 155 | |
| 156 | // Convert is a convenience function which removes the need to handle the |
| 157 | // boolean return value from FromError. |
| 158 | func Convert(err error) *Status { |
| 159 | s, _ := FromError(err) |
| 160 | return s |
| 161 | } |
| 162 | |
| 163 | // WithDetails returns a new status with the provided details messages appended to the status. |
| 164 | // If any errors are encountered, it returns nil and the first error encountered. |
| 165 | func (s *Status) WithDetails(details ...proto.Message) (*Status, error) { |
| 166 | if s.Code() == codes.OK { |
| 167 | return nil, errors.New("no error details for status with code OK") |
| 168 | } |
| 169 | // s.Code() != OK implies that s.Proto() != nil. |
| 170 | p := s.Proto() |
| 171 | for _, detail := range details { |
| 172 | any, err := ptypes.MarshalAny(detail) |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | p.Details = append(p.Details, any) |
| 177 | } |
| 178 | return &Status{s: p}, nil |
| 179 | } |
| 180 | |
| 181 | // Details returns a slice of details messages attached to the status. |
| 182 | // If a detail cannot be decoded, the error is returned in place of the detail. |
| 183 | func (s *Status) Details() []interface{} { |
| 184 | if s == nil || s.s == nil { |
| 185 | return nil |
| 186 | } |
| 187 | details := make([]interface{}, 0, len(s.s.Details)) |
| 188 | for _, any := range s.s.Details { |
| 189 | detail := &ptypes.DynamicAny{} |
| 190 | if err := ptypes.UnmarshalAny(any, detail); err != nil { |
| 191 | details = append(details, err) |
| 192 | continue |
| 193 | } |
| 194 | details = append(details, detail.Message) |
| 195 | } |
| 196 | return details |
| 197 | } |
| 198 | |
| 199 | // Code returns the Code of the error if it is a Status error, codes.OK if err |
| 200 | // is nil, or codes.Unknown otherwise. |
| 201 | func Code(err error) codes.Code { |
| 202 | // Don't use FromError to avoid allocation of OK status. |
| 203 | if err == nil { |
| 204 | return codes.OK |
| 205 | } |
| 206 | if se, ok := err.(interface { |
| 207 | GRPCStatus() *Status |
| 208 | }); ok { |
| 209 | return se.GRPCStatus().Code() |
| 210 | } |
| 211 | return codes.Unknown |
| 212 | } |
| 213 | |
| 214 | // FromContextError converts a context error into a Status. It returns a |
| 215 | // Status with codes.OK if err is nil, or a Status with codes.Unknown if err is |
| 216 | // non-nil and not a context error. |
| 217 | func FromContextError(err error) *Status { |
| 218 | switch err { |
| 219 | case nil: |
| 220 | return nil |
| 221 | case context.DeadlineExceeded: |
| 222 | return New(codes.DeadlineExceeded, err.Error()) |
| 223 | case context.Canceled: |
| 224 | return New(codes.Canceled, err.Error()) |
| 225 | default: |
| 226 | return New(codes.Unknown, err.Error()) |
| 227 | } |
| 228 | } |