3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 "github.com/golang/protobuf/proto"
28 "github.com/golang/protobuf/ptypes"
29 apb "github.com/golang/protobuf/ptypes/any"
30 dpb "github.com/golang/protobuf/ptypes/duration"
31 cpb "google.golang.org/genproto/googleapis/rpc/code"
32 epb "google.golang.org/genproto/googleapis/rpc/errdetails"
33 spb "google.golang.org/genproto/googleapis/rpc/status"
34 "google.golang.org/grpc/codes"
37 func TestErrorsWithSameParameters(t *testing.T) {
38 const description = "some description"
39 e1 := Errorf(codes.AlreadyExists, description)
40 e2 := Errorf(codes.AlreadyExists, description)
41 if e1 == e2 || !reflect.DeepEqual(e1, e2) {
42 t.Fatalf("Errors should be equivalent but unique - e1: %v, %v e2: %p, %v", e1.(*statusError), e1, e2.(*statusError), e2)
46 func TestFromToProto(t *testing.T) {
48 Code: int32(codes.Internal),
49 Message: "test test test",
50 Details: []*apb.Any{{TypeUrl: "foo", Value: []byte{3, 2, 1}}},
54 if got := err.Proto(); !proto.Equal(s, got) {
55 t.Fatalf("Expected errors to be identical - s: %v got: %v", s, got)
59 func TestFromNilProto(t *testing.T) {
60 tests := []*Status{nil, FromProto(nil)}
61 for _, s := range tests {
62 if c := s.Code(); c != codes.OK {
63 t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
65 if m := s.Message(); m != "" {
66 t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
68 if p := s.Proto(); p != nil {
69 t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
71 if e := s.Err(); e != nil {
72 t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
77 func TestError(t *testing.T) {
78 err := Error(codes.Internal, "test description")
79 if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {
80 t.Fatalf("err.Error() = %q; want %q", got, want)
82 s, _ := FromError(err)
83 if got, want := s.Code(), codes.Internal; got != want {
84 t.Fatalf("err.Code() = %s; want %s", got, want)
86 if got, want := s.Message(), "test description"; got != want {
87 t.Fatalf("err.Message() = %s; want %s", got, want)
91 func TestErrorOK(t *testing.T) {
92 err := Error(codes.OK, "foo")
94 t.Fatalf("Error(codes.OK, _) = %p; want nil", err.(*statusError))
98 func TestErrorProtoOK(t *testing.T) {
99 s := &spb.Status{Code: int32(codes.OK)}
100 if got := ErrorProto(s); got != nil {
101 t.Fatalf("ErrorProto(%v) = %v; want nil", s, got)
105 func TestFromError(t *testing.T) {
106 code, message := codes.Internal, "test description"
107 err := Error(code, message)
108 s, ok := FromError(err)
109 if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
110 t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
114 func TestFromErrorOK(t *testing.T) {
115 code, message := codes.OK, ""
116 s, ok := FromError(nil)
117 if !ok || s.Code() != code || s.Message() != message || s.Err() != nil {
118 t.Fatalf("FromError(nil) = %v, %v; want <Code()=%s, Message()=%q, Err=nil>, true", s, ok, code, message)
122 func TestStatus_ErrorDetails(t *testing.T) {
125 details []proto.Message
128 code: codes.NotFound,
132 code: codes.NotFound,
133 details: []proto.Message{
135 ResourceType: "book",
136 ResourceName: "projects/1234/books/5678",
142 code: codes.Internal,
143 details: []proto.Message{
145 StackEntries: []string{
153 code: codes.Unavailable,
154 details: []proto.Message{
156 RetryDelay: &dpb.Duration{Seconds: 60},
159 ResourceType: "book",
160 ResourceName: "projects/1234/books/5678",
167 for _, tc := range tests {
168 s, err := New(tc.code, "").WithDetails(tc.details...)
170 t.Fatalf("(%v).WithDetails(%+v) failed: %v", str(s), tc.details, err)
172 details := s.Details()
173 for i := range details {
174 if !proto.Equal(details[i].(proto.Message), tc.details[i]) {
175 t.Fatalf("(%v).Details()[%d] = %+v, want %+v", str(s), i, details[i], tc.details[i])
181 func TestStatus_WithDetails_Fail(t *testing.T) {
187 for _, s := range tests {
188 if s, err := s.WithDetails(); err == nil || s != nil {
189 t.Fatalf("(%v).WithDetails(%+v) = %v, %v; want nil, non-nil", str(s), []proto.Message{}, s, err)
194 func TestStatus_ErrorDetails_Fail(t *testing.T) {
212 FromProto(&spb.Status{
213 Code: int32(cpb.Code_CANCELLED),
219 mustMarshalAny(&epb.ResourceInfo{
220 ResourceType: "book",
221 ResourceName: "projects/1234/books/5678",
227 errors.New(`message type url "" is invalid`),
229 ResourceType: "book",
230 ResourceName: "projects/1234/books/5678",
236 for _, tc := range tests {
237 got := tc.s.Details()
238 if !reflect.DeepEqual(got, tc.i) {
239 t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
244 func str(s *Status) string {
251 return fmt.Sprintf("<Code=%v, Message=%q, Details=%+v>", codes.Code(s.s.GetCode()), s.s.GetMessage(), s.s.GetDetails())
254 // mustMarshalAny converts a protobuf message to an any.
255 func mustMarshalAny(msg proto.Message) *apb.Any {
256 any, err := ptypes.MarshalAny(msg)
258 panic(fmt.Sprintf("ptypes.MarshalAny(%+v) failed: %v", msg, err))