status_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. package status
  19. import (
  20. "context"
  21. "errors"
  22. "fmt"
  23. "testing"
  24. "github.com/golang/protobuf/proto"
  25. "github.com/golang/protobuf/ptypes"
  26. apb "github.com/golang/protobuf/ptypes/any"
  27. dpb "github.com/golang/protobuf/ptypes/duration"
  28. "github.com/google/go-cmp/cmp"
  29. cpb "google.golang.org/genproto/googleapis/rpc/code"
  30. epb "google.golang.org/genproto/googleapis/rpc/errdetails"
  31. spb "google.golang.org/genproto/googleapis/rpc/status"
  32. "google.golang.org/grpc/codes"
  33. "google.golang.org/grpc/internal/grpctest"
  34. "google.golang.org/grpc/internal/status"
  35. )
  36. type s struct {
  37. grpctest.Tester
  38. }
  39. func Test(t *testing.T) {
  40. grpctest.RunSubTests(t, s{})
  41. }
  42. // errEqual is essentially a copy of testutils.StatusErrEqual(), to avoid a
  43. // cyclic dependency.
  44. func errEqual(err1, err2 error) bool {
  45. status1, ok := FromError(err1)
  46. if !ok {
  47. return false
  48. }
  49. status2, ok := FromError(err2)
  50. if !ok {
  51. return false
  52. }
  53. return proto.Equal(status1.Proto(), status2.Proto())
  54. }
  55. func (s) TestErrorsWithSameParameters(t *testing.T) {
  56. const description = "some description"
  57. e1 := Errorf(codes.AlreadyExists, description)
  58. e2 := Errorf(codes.AlreadyExists, description)
  59. if e1 == e2 || !errEqual(e1, e2) {
  60. t.Fatalf("Errors should be equivalent but unique - e1: %v, %v e2: %p, %v", e1.(*status.Error), e1, e2.(*status.Error), e2)
  61. }
  62. }
  63. func (s) TestFromToProto(t *testing.T) {
  64. s := &spb.Status{
  65. Code: int32(codes.Internal),
  66. Message: "test test test",
  67. Details: []*apb.Any{{TypeUrl: "foo", Value: []byte{3, 2, 1}}},
  68. }
  69. err := FromProto(s)
  70. if got := err.Proto(); !proto.Equal(s, got) {
  71. t.Fatalf("Expected errors to be identical - s: %v got: %v", s, got)
  72. }
  73. }
  74. func (s) TestFromNilProto(t *testing.T) {
  75. tests := []*Status{nil, FromProto(nil)}
  76. for _, s := range tests {
  77. if c := s.Code(); c != codes.OK {
  78. t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
  79. }
  80. if m := s.Message(); m != "" {
  81. t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
  82. }
  83. if p := s.Proto(); p != nil {
  84. t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
  85. }
  86. if e := s.Err(); e != nil {
  87. t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
  88. }
  89. }
  90. }
  91. func (s) TestError(t *testing.T) {
  92. err := Error(codes.Internal, "test description")
  93. if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {
  94. t.Fatalf("err.Error() = %q; want %q", got, want)
  95. }
  96. s, _ := FromError(err)
  97. if got, want := s.Code(), codes.Internal; got != want {
  98. t.Fatalf("err.Code() = %s; want %s", got, want)
  99. }
  100. if got, want := s.Message(), "test description"; got != want {
  101. t.Fatalf("err.Message() = %s; want %s", got, want)
  102. }
  103. }
  104. func (s) TestErrorOK(t *testing.T) {
  105. err := Error(codes.OK, "foo")
  106. if err != nil {
  107. t.Fatalf("Error(codes.OK, _) = %p; want nil", err.(*status.Error))
  108. }
  109. }
  110. func (s) TestErrorProtoOK(t *testing.T) {
  111. s := &spb.Status{Code: int32(codes.OK)}
  112. if got := ErrorProto(s); got != nil {
  113. t.Fatalf("ErrorProto(%v) = %v; want nil", s, got)
  114. }
  115. }
  116. func (s) TestFromError(t *testing.T) {
  117. code, message := codes.Internal, "test description"
  118. err := Error(code, message)
  119. s, ok := FromError(err)
  120. if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
  121. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
  122. }
  123. }
  124. func (s) TestFromErrorOK(t *testing.T) {
  125. code, message := codes.OK, ""
  126. s, ok := FromError(nil)
  127. if !ok || s.Code() != code || s.Message() != message || s.Err() != nil {
  128. t.Fatalf("FromError(nil) = %v, %v; want <Code()=%s, Message()=%q, Err=nil>, true", s, ok, code, message)
  129. }
  130. }
  131. type customError struct {
  132. Code codes.Code
  133. Message string
  134. Details []*apb.Any
  135. }
  136. func (c customError) Error() string {
  137. return fmt.Sprintf("rpc error: code = %s desc = %s", c.Code, c.Message)
  138. }
  139. func (c customError) GRPCStatus() *Status {
  140. return status.FromProto(&spb.Status{
  141. Code: int32(c.Code),
  142. Message: c.Message,
  143. Details: c.Details,
  144. })
  145. }
  146. func (s) TestFromErrorImplementsInterface(t *testing.T) {
  147. code, message := codes.Internal, "test description"
  148. details := []*apb.Any{{
  149. TypeUrl: "testUrl",
  150. Value: []byte("testValue"),
  151. }}
  152. err := customError{
  153. Code: code,
  154. Message: message,
  155. Details: details,
  156. }
  157. s, ok := FromError(err)
  158. if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
  159. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
  160. }
  161. pd := s.Proto().GetDetails()
  162. if len(pd) != 1 || !proto.Equal(pd[0], details[0]) {
  163. t.Fatalf("s.Proto.GetDetails() = %v; want <Details()=%s>", pd, details)
  164. }
  165. }
  166. func (s) TestFromErrorUnknownError(t *testing.T) {
  167. code, message := codes.Unknown, "unknown error"
  168. err := errors.New("unknown error")
  169. s, ok := FromError(err)
  170. if ok || s.Code() != code || s.Message() != message {
  171. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q>, false", err, s, ok, code, message)
  172. }
  173. }
  174. func (s) TestConvertKnownError(t *testing.T) {
  175. code, message := codes.Internal, "test description"
  176. err := Error(code, message)
  177. s := Convert(err)
  178. if s.Code() != code || s.Message() != message {
  179. t.Fatalf("Convert(%v) = %v; want <Code()=%s, Message()=%q>", err, s, code, message)
  180. }
  181. }
  182. func (s) TestConvertUnknownError(t *testing.T) {
  183. code, message := codes.Unknown, "unknown error"
  184. err := errors.New("unknown error")
  185. s := Convert(err)
  186. if s.Code() != code || s.Message() != message {
  187. t.Fatalf("Convert(%v) = %v; want <Code()=%s, Message()=%q>", err, s, code, message)
  188. }
  189. }
  190. func (s) TestStatus_ErrorDetails(t *testing.T) {
  191. tests := []struct {
  192. code codes.Code
  193. details []proto.Message
  194. }{
  195. {
  196. code: codes.NotFound,
  197. details: nil,
  198. },
  199. {
  200. code: codes.NotFound,
  201. details: []proto.Message{
  202. &epb.ResourceInfo{
  203. ResourceType: "book",
  204. ResourceName: "projects/1234/books/5678",
  205. Owner: "User",
  206. },
  207. },
  208. },
  209. {
  210. code: codes.Internal,
  211. details: []proto.Message{
  212. &epb.DebugInfo{
  213. StackEntries: []string{
  214. "first stack",
  215. "second stack",
  216. },
  217. },
  218. },
  219. },
  220. {
  221. code: codes.Unavailable,
  222. details: []proto.Message{
  223. &epb.RetryInfo{
  224. RetryDelay: &dpb.Duration{Seconds: 60},
  225. },
  226. &epb.ResourceInfo{
  227. ResourceType: "book",
  228. ResourceName: "projects/1234/books/5678",
  229. Owner: "User",
  230. },
  231. },
  232. },
  233. }
  234. for _, tc := range tests {
  235. s, err := New(tc.code, "").WithDetails(tc.details...)
  236. if err != nil {
  237. t.Fatalf("(%v).WithDetails(%+v) failed: %v", str(s), tc.details, err)
  238. }
  239. details := s.Details()
  240. for i := range details {
  241. if !proto.Equal(details[i].(proto.Message), tc.details[i]) {
  242. t.Fatalf("(%v).Details()[%d] = %+v, want %+v", str(s), i, details[i], tc.details[i])
  243. }
  244. }
  245. }
  246. }
  247. func (s) TestStatus_WithDetails_Fail(t *testing.T) {
  248. tests := []*Status{
  249. nil,
  250. FromProto(nil),
  251. New(codes.OK, ""),
  252. }
  253. for _, s := range tests {
  254. if s, err := s.WithDetails(); err == nil || s != nil {
  255. t.Fatalf("(%v).WithDetails(%+v) = %v, %v; want nil, non-nil", str(s), []proto.Message{}, s, err)
  256. }
  257. }
  258. }
  259. func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
  260. tests := []struct {
  261. s *Status
  262. i []interface{}
  263. }{
  264. {
  265. nil,
  266. nil,
  267. },
  268. {
  269. FromProto(nil),
  270. nil,
  271. },
  272. {
  273. New(codes.OK, ""),
  274. []interface{}{},
  275. },
  276. {
  277. FromProto(&spb.Status{
  278. Code: int32(cpb.Code_CANCELLED),
  279. Details: []*apb.Any{
  280. {
  281. TypeUrl: "",
  282. Value: []byte{},
  283. },
  284. mustMarshalAny(&epb.ResourceInfo{
  285. ResourceType: "book",
  286. ResourceName: "projects/1234/books/5678",
  287. Owner: "User",
  288. }),
  289. },
  290. }),
  291. []interface{}{
  292. errors.New(`message type url "" is invalid`),
  293. &epb.ResourceInfo{
  294. ResourceType: "book",
  295. ResourceName: "projects/1234/books/5678",
  296. Owner: "User",
  297. },
  298. },
  299. },
  300. }
  301. for _, tc := range tests {
  302. got := tc.s.Details()
  303. if !cmp.Equal(got, tc.i, cmp.Comparer(proto.Equal), cmp.Comparer(equalError)) {
  304. t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
  305. }
  306. }
  307. }
  308. func equalError(x, y error) bool {
  309. return x == y || (x != nil && y != nil && x.Error() == y.Error())
  310. }
  311. func str(s *Status) string {
  312. if s == nil {
  313. return "nil"
  314. }
  315. if s.Proto() == nil {
  316. return "<Code=OK>"
  317. }
  318. return fmt.Sprintf("<Code=%v, Message=%q, Details=%+v>", s.Code(), s.Message(), s.Details())
  319. }
  320. // mustMarshalAny converts a protobuf message to an any.
  321. func mustMarshalAny(msg proto.Message) *apb.Any {
  322. any, err := ptypes.MarshalAny(msg)
  323. if err != nil {
  324. panic(fmt.Sprintf("ptypes.MarshalAny(%+v) failed: %v", msg, err))
  325. }
  326. return any
  327. }
  328. func (s) TestFromContextError(t *testing.T) {
  329. testCases := []struct {
  330. in error
  331. want *Status
  332. }{
  333. {in: nil, want: New(codes.OK, "")},
  334. {in: context.DeadlineExceeded, want: New(codes.DeadlineExceeded, context.DeadlineExceeded.Error())},
  335. {in: context.Canceled, want: New(codes.Canceled, context.Canceled.Error())},
  336. {in: errors.New("other"), want: New(codes.Unknown, "other")},
  337. }
  338. for _, tc := range testCases {
  339. got := FromContextError(tc.in)
  340. if got.Code() != tc.want.Code() || got.Message() != tc.want.Message() {
  341. t.Errorf("FromContextError(%v) = %v; want %v", tc.in, got, tc.want)
  342. }
  343. }
  344. }