status.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "fmt"
  31. spb "google.golang.org/genproto/googleapis/rpc/status"
  32. "google.golang.org/grpc/codes"
  33. "google.golang.org/grpc/internal/status"
  34. )
  35. // Status references google.golang.org/grpc/internal/status. It represents an
  36. // RPC status code, message, and details. It is immutable and should be
  37. // created with New, Newf, or FromProto.
  38. // https://godoc.org/google.golang.org/grpc/internal/status
  39. type Status = status.Status
  40. // New returns a Status representing c and msg.
  41. func New(c codes.Code, msg string) *Status {
  42. return status.New(c, msg)
  43. }
  44. // Newf returns New(c, fmt.Sprintf(format, a...)).
  45. func Newf(c codes.Code, format string, a ...interface{}) *Status {
  46. return New(c, fmt.Sprintf(format, a...))
  47. }
  48. // Error returns an error representing c and msg. If c is OK, returns nil.
  49. func Error(c codes.Code, msg string) error {
  50. return New(c, msg).Err()
  51. }
  52. // Errorf returns Error(c, fmt.Sprintf(format, a...)).
  53. func Errorf(c codes.Code, format string, a ...interface{}) error {
  54. return Error(c, fmt.Sprintf(format, a...))
  55. }
  56. // ErrorProto returns an error representing s. If s.Code is OK, returns nil.
  57. func ErrorProto(s *spb.Status) error {
  58. return FromProto(s).Err()
  59. }
  60. // FromProto returns a Status representing s.
  61. func FromProto(s *spb.Status) *Status {
  62. return status.FromProto(s)
  63. }
  64. // FromError returns a Status representing err if it was produced from this
  65. // package or has a method `GRPCStatus() *Status`. Otherwise, ok is false and a
  66. // Status is returned with codes.Unknown and the original error message.
  67. func FromError(err error) (s *Status, ok bool) {
  68. if err == nil {
  69. return nil, true
  70. }
  71. if se, ok := err.(interface {
  72. GRPCStatus() *Status
  73. }); ok {
  74. return se.GRPCStatus(), true
  75. }
  76. return New(codes.Unknown, err.Error()), false
  77. }
  78. // Convert is a convenience function which removes the need to handle the
  79. // boolean return value from FromError.
  80. func Convert(err error) *Status {
  81. s, _ := FromError(err)
  82. return s
  83. }
  84. // Code returns the Code of the error if it is a Status error, codes.OK if err
  85. // is nil, or codes.Unknown otherwise.
  86. func Code(err error) codes.Code {
  87. // Don't use FromError to avoid allocation of OK status.
  88. if err == nil {
  89. return codes.OK
  90. }
  91. if se, ok := err.(interface {
  92. GRPCStatus() *Status
  93. }); ok {
  94. return se.GRPCStatus().Code()
  95. }
  96. return codes.Unknown
  97. }
  98. // FromContextError converts a context error into a Status. It returns a
  99. // Status with codes.OK if err is nil, or a Status with codes.Unknown if err is
  100. // non-nil and not a context error.
  101. func FromContextError(err error) *Status {
  102. switch err {
  103. case nil:
  104. return nil
  105. case context.DeadlineExceeded:
  106. return New(codes.DeadlineExceeded, err.Error())
  107. case context.Canceled:
  108. return New(codes.Canceled, err.Error())
  109. default:
  110. return New(codes.Unknown, err.Error())
  111. }
  112. }