1 /*
2 *
3 * Copyright 2020 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 "errors"
32 "fmt"
33 34 spb "google.golang.org/genproto/googleapis/rpc/status"
35 "google.golang.org/grpc/codes"
36 "google.golang.org/protobuf/proto"
37 "google.golang.org/protobuf/protoadapt"
38 "google.golang.org/protobuf/types/known/anypb"
39 )
40 41 // Status represents an RPC status code, message, and details. It is immutable
42 // and should be created with New, Newf, or FromProto.
43 type Status struct {
44 s *spb.Status
45 }
46 47 // NewWithProto returns a new status including details from statusProto. This
48 // is meant to be used by the gRPC library only.
49 func NewWithProto(code codes.Code, message string, statusProto []string) *Status {
50 if len(statusProto) != 1 {
51 // No grpc-status-details bin header, or multiple; just ignore.
52 return &Status{s: &spb.Status{Code: int32(code), Message: message}}
53 }
54 st := &spb.Status{}
55 if err := proto.Unmarshal([]byte(statusProto[0]), st); err != nil {
56 // Probably not a google.rpc.Status proto; do not provide details.
57 return &Status{s: &spb.Status{Code: int32(code), Message: message}}
58 }
59 if st.Code == int32(code) {
60 // The codes match between the grpc-status header and the
61 // grpc-status-details-bin header; use the full details proto.
62 return &Status{s: st}
63 }
64 return &Status{
65 s: &spb.Status{
66 Code: int32(codes.Internal),
67 Message: fmt.Sprintf(
68 "grpc-status-details-bin mismatch: grpc-status=%v, grpc-message=%q, grpc-status-details-bin=%+v",
69 code, message, st,
70 ),
71 },
72 }
73 }
74 75 // New returns a Status representing c and msg.
76 func New(c codes.Code, msg string) *Status {
77 return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
78 }
79 80 // Newf returns New(c, fmt.Sprintf(format, a...)).
81 func Newf(c codes.Code, format string, a ...any) *Status {
82 return New(c, fmt.Sprintf(format, a...))
83 }
84 85 // FromProto returns a Status representing s.
86 func FromProto(s *spb.Status) *Status {
87 return &Status{s: proto.Clone(s).(*spb.Status)}
88 }
89 90 // Err returns an error representing c and msg. If c is OK, returns nil.
91 func Err(c codes.Code, msg string) error {
92 return New(c, msg).Err()
93 }
94 95 // Errorf returns Error(c, fmt.Sprintf(format, a...)).
96 func Errorf(c codes.Code, format string, a ...any) error {
97 return Err(c, fmt.Sprintf(format, a...))
98 }
99 100 // Code returns the status code contained in s.
101 func (s *Status) Code() codes.Code {
102 if s == nil || s.s == nil {
103 return codes.OK
104 }
105 return codes.Code(s.s.Code)
106 }
107 108 // Message returns the message contained in s.
109 func (s *Status) Message() string {
110 if s == nil || s.s == nil {
111 return ""
112 }
113 return s.s.Message
114 }
115 116 // Proto returns s's status as an spb.Status proto message.
117 func (s *Status) Proto() *spb.Status {
118 if s == nil {
119 return nil
120 }
121 return proto.Clone(s.s).(*spb.Status)
122 }
123 124 // Err returns an immutable error representing s; returns nil if s.Code() is OK.
125 func (s *Status) Err() error {
126 if s.Code() == codes.OK {
127 return nil
128 }
129 return &Error{s: s}
130 }
131 132 // WithDetails returns a new status with the provided details messages appended to the status.
133 // If any errors are encountered, it returns nil and the first error encountered.
134 func (s *Status) WithDetails(details ...protoadapt.MessageV1) (*Status, error) {
135 if s.Code() == codes.OK {
136 return nil, errors.New("no error details for status with code OK")
137 }
138 // s.Code() != OK implies that s.Proto() != nil.
139 p := s.Proto()
140 for _, detail := range details {
141 m, err := anypb.New(protoadapt.MessageV2Of(detail))
142 if err != nil {
143 return nil, err
144 }
145 p.Details = append(p.Details, m)
146 }
147 return &Status{s: p}, nil
148 }
149 150 // Details returns a slice of details messages attached to the status.
151 // If a detail cannot be decoded, the error is returned in place of the detail.
152 // If the detail can be decoded, the proto message returned is of the same
153 // type that was given to WithDetails().
154 func (s *Status) Details() []any {
155 if s == nil || s.s == nil {
156 return nil
157 }
158 details := make([]any, 0, len(s.s.Details))
159 for _, any := range s.s.Details {
160 detail, err := any.UnmarshalNew()
161 if err != nil {
162 details = append(details, err)
163 continue
164 }
165 // The call to MessageV1Of is required to unwrap the proto message if
166 // it implemented only the MessageV1 API. The proto message would have
167 // been wrapped in a V2 wrapper in Status.WithDetails. V2 messages are
168 // added to a global registry used by any.UnmarshalNew().
169 // MessageV1Of has the following behaviour:
170 // 1. If the given message is a wrapped MessageV1, it returns the
171 // unwrapped value.
172 // 2. If the given message already implements MessageV1, it returns it
173 // as is.
174 // 3. Else, it wraps the MessageV2 in a MessageV1 wrapper.
175 //
176 // Since the Status.WithDetails() API only accepts MessageV1, calling
177 // MessageV1Of ensures we return the same type that was given to
178 // WithDetails:
179 // * If the give type implemented only MessageV1, the unwrapping from
180 // point 1 above will restore the type.
181 // * If the given type implemented both MessageV1 and MessageV2, point 2
182 // above will ensure no wrapping is performed.
183 // * If the given type implemented only MessageV2 and was wrapped using
184 // MessageV1Of before passing to WithDetails(), it would be unwrapped
185 // in WithDetails by calling MessageV2Of(). Point 3 above will ensure
186 // that the type is wrapped in a MessageV1 wrapper again before
187 // returning. Note that protoc-gen-go doesn't generate code which
188 // implements ONLY MessageV2 at the time of writing.
189 //
190 // NOTE: Status details can also be added using the FromProto method.
191 // This could theoretically allow passing a Detail message that only
192 // implements the V2 API. In such a case the message will be wrapped in
193 // a MessageV1 wrapper when fetched using Details().
194 // Since protoc-gen-go generates only code that implements both V1 and
195 // V2 APIs for backward compatibility, this is not a concern.
196 details = append(details, protoadapt.MessageV1Of(detail))
197 }
198 return details
199 }
200 201 func (s *Status) String() string {
202 return fmt.Sprintf("rpc error: code = %s desc = %s", s.Code(), s.Message())
203 }
204 205 // Error wraps a pointer of a status proto. It implements error and Status,
206 // and a nil *Error should never be returned by this package.
207 type Error struct {
208 s *Status
209 }
210 211 func (e *Error) Error() string {
212 return e.s.String()
213 }
214 215 // GRPCStatus returns the Status represented by se.
216 func (e *Error) GRPCStatus() *Status {
217 return e.s
218 }
219 220 // Is implements future error.Is functionality.
221 // A Error is equivalent if the code and message are identical.
222 func (e *Error) Is(target error) bool {
223 tse, ok := target.(*Error)
224 if !ok {
225 return false
226 }
227 return proto.Equal(e.s.s, tse.s.s)
228 }
229 230 // IsRestrictedControlPlaneCode returns whether the status includes a code
231 // restricted for control plane usage as defined by gRFC A54.
232 func IsRestrictedControlPlaneCode(s *Status) bool {
233 switch s.Code() {
234 case codes.InvalidArgument, codes.NotFound, codes.AlreadyExists, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.DataLoss:
235 return true
236 }
237 return false
238 }
239 240 // RawStatusProto returns the internal protobuf message for use by gRPC itself.
241 func RawStatusProto(s *Status) *spb.Status {
242 if s == nil {
243 return nil
244 }
245 return s.s
246 }
247