/go/vendor/google.golang.org/grpc/status/status.go

https://gitlab.com/ipernet/gitlab-shell · Go · 193 lines · 117 code · 22 blank · 54 comment · 30 complexity · 340890095d47157f1b7be4cd33320614 MD5 · raw file

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