status.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 implements errors returned by gRPC. These errors are
  19. // serialized and transmitted on the wire between server and client, and allow
  20. // for additional data to be transmitted via the Details field in the status
  21. // proto. gRPC service handlers should return an error created by this
  22. // package, and gRPC clients should expect a corresponding error to be
  23. // returned from the RPC call.
  24. //
  25. // This package upholds the invariants that a non-nil error may not
  26. // contain an OK code, and an OK code must result in a nil error.
  27. package status
  28. import (
  29. "context"
  30. "errors"
  31. "fmt"
  32. spb "google.golang.org/genproto/googleapis/rpc/status"
  33. "google.golang.org/grpc/codes"
  34. "google.golang.org/grpc/internal/status"
  35. )
  36. // Status references google.golang.org/grpc/internal/status. It represents an
  37. // RPC status code, message, and details. It is immutable and should be
  38. // created with New, Newf, or FromProto.
  39. // https://godoc.org/google.golang.org/grpc/internal/status
  40. type Status = status.Status
  41. // New returns a Status representing c and msg.
  42. func New(c codes.Code, msg string) *Status {
  43. return status.New(c, msg)
  44. }
  45. // Newf returns New(c, fmt.Sprintf(format, a...)).
  46. func Newf(c codes.Code, format string, a ...interface{}) *Status {
  47. return New(c, fmt.Sprintf(format, a...))
  48. }
  49. // Error returns an error representing c and msg. If c is OK, returns nil.
  50. func Error(c codes.Code, msg string) error {
  51. return New(c, msg).Err()
  52. }
  53. // Errorf returns Error(c, fmt.Sprintf(format, a...)).
  54. func Errorf(c codes.Code, format string, a ...interface{}) error {
  55. return Error(c, fmt.Sprintf(format, a...))
  56. }
  57. // ErrorProto returns an error representing s. If s.Code is OK, returns nil.
  58. func ErrorProto(s *spb.Status) error {
  59. return FromProto(s).Err()
  60. }
  61. // FromProto returns a Status representing s.
  62. func FromProto(s *spb.Status) *Status {
  63. return status.FromProto(s)
  64. }
  65. // FromError returns a Status representation of err.
  66. //
  67. // - If err was produced by this package or implements the method `GRPCStatus()
  68. // *Status` and `GRPCStatus()` does not return nil, or if err wraps a type
  69. // satisfying this, the Status from `GRPCStatus()` is returned. For wrapped
  70. // errors, the message returned contains the entire err.Error() text and not
  71. // just the wrapped status. In that case, ok is true.
  72. //
  73. // - If err is nil, a Status is returned with codes.OK and no message, and ok
  74. // is true.
  75. //
  76. // - If err implements the method `GRPCStatus() *Status` and `GRPCStatus()`
  77. // returns nil (which maps to Codes.OK), or if err wraps a type
  78. // satisfying this, a Status is returned with codes.Unknown and err's
  79. // Error() message, and ok is false.
  80. //
  81. // - Otherwise, err is an error not compatible with this package. In this
  82. // case, a Status is returned with codes.Unknown and err's Error() message,
  83. // and ok is false.
  84. func FromError(err error) (s *Status, ok bool) {
  85. if err == nil {
  86. return nil, true
  87. }
  88. type grpcstatus interface{ GRPCStatus() *Status }
  89. if gs, ok := err.(grpcstatus); ok {
  90. if gs.GRPCStatus() == nil {
  91. // Error has status nil, which maps to codes.OK. There
  92. // is no sensible behavior for this, so we turn it into
  93. // an error with codes.Unknown and discard the existing
  94. // status.
  95. return New(codes.Unknown, err.Error()), false
  96. }
  97. return gs.GRPCStatus(), true
  98. }
  99. var gs grpcstatus
  100. if errors.As(err, &gs) {
  101. if gs.GRPCStatus() == nil {
  102. // Error wraps an error that has status nil, which maps
  103. // to codes.OK. There is no sensible behavior for this,
  104. // so we turn it into an error with codes.Unknown and
  105. // discard the existing status.
  106. return New(codes.Unknown, err.Error()), false
  107. }
  108. p := gs.GRPCStatus().Proto()
  109. p.Message = err.Error()
  110. return status.FromProto(p), true
  111. }
  112. return New(codes.Unknown, err.Error()), false
  113. }
  114. // Convert is a convenience function which removes the need to handle the
  115. // boolean return value from FromError.
  116. func Convert(err error) *Status {
  117. s, _ := FromError(err)
  118. return s
  119. }
  120. // Code returns the Code of the error if it is a Status error or if it wraps a
  121. // Status error. If that is not the case, it returns codes.OK if err is nil, or
  122. // codes.Unknown otherwise.
  123. func Code(err error) codes.Code {
  124. // Don't use FromError to avoid allocation of OK status.
  125. if err == nil {
  126. return codes.OK
  127. }
  128. return Convert(err).Code()
  129. }
  130. // FromContextError converts a context error or wrapped context error into a
  131. // Status. It returns a Status with codes.OK if err is nil, or a Status with
  132. // codes.Unknown if err is non-nil and not a context error.
  133. func FromContextError(err error) *Status {
  134. if err == nil {
  135. return nil
  136. }
  137. if errors.Is(err, context.DeadlineExceeded) {
  138. return New(codes.DeadlineExceeded, err.Error())
  139. }
  140. if errors.Is(err, context.Canceled) {
  141. return New(codes.Canceled, err.Error())
  142. }
  143. return New(codes.Unknown, err.Error())
  144. }