status.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 by this
  65. // package or has a method `GRPCStatus() *Status`.
  66. // If err is nil, a Status is returned with codes.OK and no message.
  67. // Otherwise, ok is false and a Status is returned with codes.Unknown and
  68. // the original error message.
  69. func FromError(err error) (s *Status, ok bool) {
  70. if err == nil {
  71. return nil, true
  72. }
  73. if se, ok := err.(interface {
  74. GRPCStatus() *Status
  75. }); ok {
  76. return se.GRPCStatus(), true
  77. }
  78. return New(codes.Unknown, err.Error()), false
  79. }
  80. // Convert is a convenience function which removes the need to handle the
  81. // boolean return value from FromError.
  82. func Convert(err error) *Status {
  83. s, _ := FromError(err)
  84. return s
  85. }
  86. // Code returns the Code of the error if it is a Status error, codes.OK if err
  87. // is nil, or codes.Unknown otherwise.
  88. func Code(err error) codes.Code {
  89. // Don't use FromError to avoid allocation of OK status.
  90. if err == nil {
  91. return codes.OK
  92. }
  93. if se, ok := err.(interface {
  94. GRPCStatus() *Status
  95. }); ok {
  96. return se.GRPCStatus().Code()
  97. }
  98. return codes.Unknown
  99. }
  100. // FromContextError converts a context error into a Status. It returns a
  101. // Status with codes.OK if err is nil, or a Status with codes.Unknown if err is
  102. // non-nil and not a context error.
  103. func FromContextError(err error) *Status {
  104. switch err {
  105. case nil:
  106. return nil
  107. case context.DeadlineExceeded:
  108. return New(codes.DeadlineExceeded, err.Error())
  109. case context.Canceled:
  110. return New(codes.Canceled, err.Error())
  111. default:
  112. return New(codes.Unknown, err.Error())
  113. }
  114. }