grpc_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package errcode
  2. import (
  3. "errors"
  4. "net/http"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. )
  10. func TestCodeFromGrpcError(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. code codes.Code
  14. want int
  15. }{
  16. {
  17. name: "OK",
  18. code: codes.OK,
  19. want: http.StatusOK,
  20. },
  21. {
  22. name: "Invalid argument",
  23. code: codes.InvalidArgument,
  24. want: http.StatusBadRequest,
  25. },
  26. {
  27. name: "Failed precondition",
  28. code: codes.FailedPrecondition,
  29. want: http.StatusBadRequest,
  30. },
  31. {
  32. name: "Out of range",
  33. code: codes.OutOfRange,
  34. want: http.StatusBadRequest,
  35. },
  36. {
  37. name: "Unauthorized",
  38. code: codes.Unauthenticated,
  39. want: http.StatusUnauthorized,
  40. },
  41. {
  42. name: "Permission denied",
  43. code: codes.PermissionDenied,
  44. want: http.StatusForbidden,
  45. },
  46. {
  47. name: "Not found",
  48. code: codes.NotFound,
  49. want: http.StatusNotFound,
  50. },
  51. {
  52. name: "Canceled",
  53. code: codes.Canceled,
  54. want: http.StatusRequestTimeout,
  55. },
  56. {
  57. name: "Already exists",
  58. code: codes.AlreadyExists,
  59. want: http.StatusConflict,
  60. },
  61. {
  62. name: "Aborted",
  63. code: codes.Aborted,
  64. want: http.StatusConflict,
  65. },
  66. {
  67. name: "Resource exhausted",
  68. code: codes.ResourceExhausted,
  69. want: http.StatusTooManyRequests,
  70. },
  71. {
  72. name: "Internal",
  73. code: codes.Internal,
  74. want: http.StatusInternalServerError,
  75. },
  76. {
  77. name: "Data loss",
  78. code: codes.DataLoss,
  79. want: http.StatusInternalServerError,
  80. },
  81. {
  82. name: "Unknown",
  83. code: codes.Unknown,
  84. want: http.StatusInternalServerError,
  85. },
  86. {
  87. name: "Unimplemented",
  88. code: codes.Unimplemented,
  89. want: http.StatusNotImplemented,
  90. },
  91. {
  92. name: "Unavailable",
  93. code: codes.Unavailable,
  94. want: http.StatusServiceUnavailable,
  95. },
  96. {
  97. name: "Deadline exceeded",
  98. code: codes.DeadlineExceeded,
  99. want: http.StatusGatewayTimeout,
  100. },
  101. {
  102. name: "Beyond defined error",
  103. code: codes.Code(^uint32(0)),
  104. want: http.StatusInternalServerError,
  105. },
  106. }
  107. for _, test := range tests {
  108. test := test
  109. t.Run(test.name, func(t *testing.T) {
  110. assert.Equal(t, test.want, CodeFromGrpcError(status.Error(test.code, "foo")))
  111. })
  112. }
  113. }
  114. func TestIsGrpcError(t *testing.T) {
  115. assert.True(t, IsGrpcError(status.Error(codes.Unknown, "foo")))
  116. assert.False(t, IsGrpcError(errors.New("foo")))
  117. assert.False(t, IsGrpcError(nil))
  118. }