Skip to content

Commit c5faf56

Browse files
authored
status: move statusError to internal/status package (#3432)
1 parent 7cb4db2 commit c5faf56

File tree

5 files changed

+186
-132
lines changed

5 files changed

+186
-132
lines changed

internal/internal.go

-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ var (
3737
// KeepaliveMinPingTime is the minimum ping interval. This must be 10s by
3838
// default, but tests may wish to set it lower for convenience.
3939
KeepaliveMinPingTime = 10 * time.Second
40-
// StatusRawProto is exported by status/status.go. This func returns a
41-
// pointer to the wrapped Status proto for a given status.Status without a
42-
// call to proto.Clone(). The returned Status proto should not be mutated by
43-
// the caller.
44-
StatusRawProto interface{} // func (*status.Status) *spb.Status
4540
// NewRequestInfoContext creates a new context based on the argument context attaching
4641
// the passed in RequestInfo to the new context.
4742
NewRequestInfoContext interface{} // func(context.Context, credentials.RequestInfo) context.Context

internal/status/status.go

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
"github.com/golang/protobuf/proto"
35+
"github.com/golang/protobuf/ptypes"
36+
spb "google.golang.org/genproto/googleapis/rpc/status"
37+
"google.golang.org/grpc/codes"
38+
)
39+
40+
// Status represents an RPC status code, message, and details. It is immutable
41+
// and should be created with New, Newf, or FromProto.
42+
type Status struct {
43+
s *spb.Status
44+
}
45+
46+
// New returns a Status representing c and msg.
47+
func New(c codes.Code, msg string) *Status {
48+
return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
49+
}
50+
51+
// Newf returns New(c, fmt.Sprintf(format, a...)).
52+
func Newf(c codes.Code, format string, a ...interface{}) *Status {
53+
return New(c, fmt.Sprintf(format, a...))
54+
}
55+
56+
// FromProto returns a Status representing s.
57+
func FromProto(s *spb.Status) *Status {
58+
return &Status{s: proto.Clone(s).(*spb.Status)}
59+
}
60+
61+
// Err returns an error representing c and msg. If c is OK, returns nil.
62+
func Err(c codes.Code, msg string) error {
63+
return New(c, msg).Err()
64+
}
65+
66+
// Errorf returns Error(c, fmt.Sprintf(format, a...)).
67+
func Errorf(c codes.Code, format string, a ...interface{}) error {
68+
return Err(c, fmt.Sprintf(format, a...))
69+
}
70+
71+
// Code returns the status code contained in s.
72+
func (s *Status) Code() codes.Code {
73+
if s == nil || s.s == nil {
74+
return codes.OK
75+
}
76+
return codes.Code(s.s.Code)
77+
}
78+
79+
// Message returns the message contained in s.
80+
func (s *Status) Message() string {
81+
if s == nil || s.s == nil {
82+
return ""
83+
}
84+
return s.s.Message
85+
}
86+
87+
// Proto returns s's status as an spb.Status proto message.
88+
func (s *Status) Proto() *spb.Status {
89+
if s == nil {
90+
return nil
91+
}
92+
return proto.Clone(s.s).(*spb.Status)
93+
}
94+
95+
// Err returns an immutable error representing s; returns nil if s.Code() is OK.
96+
func (s *Status) Err() error {
97+
if s.Code() == codes.OK {
98+
return nil
99+
}
100+
return (*Error)(s.Proto())
101+
}
102+
103+
func (s *Status) Error() string {
104+
p := s.Proto()
105+
return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage())
106+
}
107+
108+
// WithDetails returns a new status with the provided details messages appended to the status.
109+
// If any errors are encountered, it returns nil and the first error encountered.
110+
func (s *Status) WithDetails(details ...proto.Message) (*Status, error) {
111+
if s.Code() == codes.OK {
112+
return nil, errors.New("no error details for status with code OK")
113+
}
114+
// s.Code() != OK implies that s.Proto() != nil.
115+
p := s.Proto()
116+
for _, detail := range details {
117+
any, err := ptypes.MarshalAny(detail)
118+
if err != nil {
119+
return nil, err
120+
}
121+
p.Details = append(p.Details, any)
122+
}
123+
return &Status{s: p}, nil
124+
}
125+
126+
// Details returns a slice of details messages attached to the status.
127+
// If a detail cannot be decoded, the error is returned in place of the detail.
128+
func (s *Status) Details() []interface{} {
129+
if s == nil || s.s == nil {
130+
return nil
131+
}
132+
details := make([]interface{}, 0, len(s.s.Details))
133+
for _, any := range s.s.Details {
134+
detail := &ptypes.DynamicAny{}
135+
if err := ptypes.UnmarshalAny(any, detail); err != nil {
136+
details = append(details, err)
137+
continue
138+
}
139+
details = append(details, detail.Message)
140+
}
141+
return details
142+
}
143+
144+
// Error is an alias of a status proto. It implements error and Status,
145+
// and a nil Error should never be returned by this package.
146+
type Error spb.Status
147+
148+
func (se *Error) Error() string {
149+
p := (*spb.Status)(se)
150+
return fmt.Sprintf("rpc error: code = %s desc = %s", codes.Code(p.GetCode()), p.GetMessage())
151+
}
152+
153+
// GRPCStatus returns the Status represented by se.
154+
func (se *Error) GRPCStatus() *Status {
155+
return FromProto((*spb.Status)(se))
156+
}
157+
158+
// Is implements future error.Is functionality.
159+
// A Error is equivalent if the code and message are identical.
160+
func (se *Error) Is(target error) bool {
161+
tse, ok := target.(*Error)
162+
if !ok {
163+
return false
164+
}
165+
return proto.Equal((*spb.Status)(se), (*spb.Status)(tse))
166+
}

internal/transport/http2_server.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ import (
3535
"golang.org/x/net/http2"
3636
"golang.org/x/net/http2/hpack"
3737

38-
spb "google.golang.org/genproto/googleapis/rpc/status"
3938
"google.golang.org/grpc/codes"
4039
"google.golang.org/grpc/credentials"
4140
"google.golang.org/grpc/grpclog"
42-
"google.golang.org/grpc/internal"
4341
"google.golang.org/grpc/internal/channelz"
4442
"google.golang.org/grpc/internal/grpcrand"
4543
"google.golang.org/grpc/keepalive"
@@ -57,9 +55,6 @@ var (
5755
// ErrHeaderListSizeLimitViolation indicates that the header list size is larger
5856
// than the limit set by peer.
5957
ErrHeaderListSizeLimitViolation = errors.New("transport: trying to send header list size larger than the limit set by peer")
60-
// statusRawProto is a function to get to the raw status proto wrapped in a
61-
// status.Status without a proto.Clone().
62-
statusRawProto = internal.StatusRawProto.(func(*status.Status) *spb.Status)
6358
)
6459

6560
// serverConnectionCounter counts the number of connections a server has seen
@@ -850,7 +845,7 @@ func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error {
850845
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status", Value: strconv.Itoa(int(st.Code()))})
851846
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-message", Value: encodeGrpcMessage(st.Message())})
852847

853-
if p := statusRawProto(st); p != nil && len(p.Details) > 0 {
848+
if p := st.Proto(); p != nil && len(p.Details) > 0 {
854849
stBytes, err := proto.Marshal(p)
855850
if err != nil {
856851
// TODO: return error instead, when callers are able to handle it.

status/status.go

+9-110
Original file line numberDiff line numberDiff line change
@@ -29,88 +29,23 @@ package status
2929

3030
import (
3131
"context"
32-
"errors"
3332
"fmt"
3433

35-
"github.com/golang/protobuf/proto"
36-
"github.com/golang/protobuf/ptypes"
3734
spb "google.golang.org/genproto/googleapis/rpc/status"
35+
3836
"google.golang.org/grpc/codes"
39-
"google.golang.org/grpc/internal"
37+
"google.golang.org/grpc/internal/status"
4038
)
4139

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-
}
40+
// Status references google.golang.org/grpc/internal/status. It represents an
41+
// RPC status code, message, and details. It is immutable and should be
42+
// created with New, Newf, or FromProto.
43+
// https://godoc.org/google.golang.org/grpc/internal/status
44+
type Status = status.Status
11045

11146
// New returns a Status representing c and msg.
11247
func New(c codes.Code, msg string) *Status {
113-
return &Status{s: &spb.Status{Code: int32(c), Message: msg}}
48+
return status.New(c, msg)
11449
}
11550

11651
// Newf returns New(c, fmt.Sprintf(format, a...)).
@@ -135,7 +70,7 @@ func ErrorProto(s *spb.Status) error {
13570

13671
// FromProto returns a Status representing s.
13772
func FromProto(s *spb.Status) *Status {
138-
return &Status{s: proto.Clone(s).(*spb.Status)}
73+
return status.FromProto(s)
13974
}
14075

14176
// FromError returns a Status representing err if it was produced from this
@@ -160,42 +95,6 @@ func Convert(err error) *Status {
16095
return s
16196
}
16297

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-
19998
// Code returns the Code of the error if it is a Status error, codes.OK if err
20099
// is nil, or codes.Unknown otherwise.
201100
func Code(err error) codes.Code {

0 commit comments

Comments
 (0)