1
18
19
20
21
22
23
24
25
26
27
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
42
43 type Status struct {
44 s *spb.Status
45 }
46
47
48
49 func NewWithProto(code codes.Code, message string, statusProto []string) *Status {
50 if len(statusProto) != 1 {
51
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
57 return &Status{s: &spb.Status{Code: int32(code), Message: message}}
58 }
59 if st.Code == int32(code) {
60
61
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
76 func New(c codes.Code, msg string) *Status {
77 return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
78 }
79
80
81 func Newf(c codes.Code, format string, a ...any) *Status {
82 return New(c, fmt.Sprintf(format, a...))
83 }
84
85
86 func FromProto(s *spb.Status) *Status {
87 return &Status{s: proto.Clone(s).(*spb.Status)}
88 }
89
90
91 func Err(c codes.Code, msg string) error {
92 return New(c, msg).Err()
93 }
94
95
96 func Errorf(c codes.Code, format string, a ...any) error {
97 return Err(c, fmt.Sprintf(format, a...))
98 }
99
100
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
109 func (s *Status) Message() string {
110 if s == nil || s.s == nil {
111 return ""
112 }
113 return s.s.Message
114 }
115
116
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
125 func (s *Status) Err() error {
126 if s.Code() == codes.OK {
127 return nil
128 }
129 return &Error{s: s}
130 }
131
132
133
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
139 p := s.Proto()
140 for _, detail := range details {
141 any, err := anypb.New(protoadapt.MessageV2Of(detail))
142 if err != nil {
143 return nil, err
144 }
145 p.Details = append(p.Details, any)
146 }
147 return &Status{s: p}, nil
148 }
149
150
151
152 func (s *Status) Details() []any {
153 if s == nil || s.s == nil {
154 return nil
155 }
156 details := make([]any, 0, len(s.s.Details))
157 for _, any := range s.s.Details {
158 detail, err := any.UnmarshalNew()
159 if err != nil {
160 details = append(details, err)
161 continue
162 }
163 details = append(details, detail)
164 }
165 return details
166 }
167
168 func (s *Status) String() string {
169 return fmt.Sprintf("rpc error: code = %s desc = %s", s.Code(), s.Message())
170 }
171
172
173
174 type Error struct {
175 s *Status
176 }
177
178 func (e *Error) Error() string {
179 return e.s.String()
180 }
181
182
183 func (e *Error) GRPCStatus() *Status {
184 return e.s
185 }
186
187
188
189 func (e *Error) Is(target error) bool {
190 tse, ok := target.(*Error)
191 if !ok {
192 return false
193 }
194 return proto.Equal(e.s.s, tse.s.s)
195 }
196
197
198
199 func IsRestrictedControlPlaneCode(s *Status) bool {
200 switch s.Code() {
201 case codes.InvalidArgument, codes.NotFound, codes.AlreadyExists, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.DataLoss:
202 return true
203 }
204 return false
205 }
206
View as plain text