codes.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. *
  3. * Copyright 2014 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 codes defines the canonical error codes used by gRPC. It is
  19. // consistent across various languages.
  20. package codes // import "google.golang.org/grpc/codes"
  21. import (
  22. "fmt"
  23. )
  24. // A Code is an unsigned 32-bit error code as defined in the gRPC spec.
  25. type Code uint32
  26. const (
  27. // OK is returned on success.
  28. OK Code = 0
  29. // Canceled indicates the operation was canceled (typically by the caller).
  30. Canceled Code = 1
  31. // Unknown error. An example of where this error may be returned is
  32. // if a Status value received from another address space belongs to
  33. // an error-space that is not known in this address space. Also
  34. // errors raised by APIs that do not return enough error information
  35. // may be converted to this error.
  36. Unknown Code = 2
  37. // InvalidArgument indicates client specified an invalid argument.
  38. // Note that this differs from FailedPrecondition. It indicates arguments
  39. // that are problematic regardless of the state of the system
  40. // (e.g., a malformed file name).
  41. InvalidArgument Code = 3
  42. // DeadlineExceeded means operation expired before completion.
  43. // For operations that change the state of the system, this error may be
  44. // returned even if the operation has completed successfully. For
  45. // example, a successful response from a server could have been delayed
  46. // long enough for the deadline to expire.
  47. DeadlineExceeded Code = 4
  48. // NotFound means some requested entity (e.g., file or directory) was
  49. // not found.
  50. NotFound Code = 5
  51. // AlreadyExists means an attempt to create an entity failed because one
  52. // already exists.
  53. AlreadyExists Code = 6
  54. // PermissionDenied indicates the caller does not have permission to
  55. // execute the specified operation. It must not be used for rejections
  56. // caused by exhausting some resource (use ResourceExhausted
  57. // instead for those errors). It must not be
  58. // used if the caller cannot be identified (use Unauthenticated
  59. // instead for those errors).
  60. PermissionDenied Code = 7
  61. // ResourceExhausted indicates some resource has been exhausted, perhaps
  62. // a per-user quota, or perhaps the entire file system is out of space.
  63. ResourceExhausted Code = 8
  64. // FailedPrecondition indicates operation was rejected because the
  65. // system is not in a state required for the operation's execution.
  66. // For example, directory to be deleted may be non-empty, an rmdir
  67. // operation is applied to a non-directory, etc.
  68. //
  69. // A litmus test that may help a service implementor in deciding
  70. // between FailedPrecondition, Aborted, and Unavailable:
  71. // (a) Use Unavailable if the client can retry just the failing call.
  72. // (b) Use Aborted if the client should retry at a higher-level
  73. // (e.g., restarting a read-modify-write sequence).
  74. // (c) Use FailedPrecondition if the client should not retry until
  75. // the system state has been explicitly fixed. E.g., if an "rmdir"
  76. // fails because the directory is non-empty, FailedPrecondition
  77. // should be returned since the client should not retry unless
  78. // they have first fixed up the directory by deleting files from it.
  79. // (d) Use FailedPrecondition if the client performs conditional
  80. // REST Get/Update/Delete on a resource and the resource on the
  81. // server does not match the condition. E.g., conflicting
  82. // read-modify-write on the same resource.
  83. FailedPrecondition Code = 9
  84. // Aborted indicates the operation was aborted, typically due to a
  85. // concurrency issue like sequencer check failures, transaction aborts,
  86. // etc.
  87. //
  88. // See litmus test above for deciding between FailedPrecondition,
  89. // Aborted, and Unavailable.
  90. Aborted Code = 10
  91. // OutOfRange means operation was attempted past the valid range.
  92. // E.g., seeking or reading past end of file.
  93. //
  94. // Unlike InvalidArgument, this error indicates a problem that may
  95. // be fixed if the system state changes. For example, a 32-bit file
  96. // system will generate InvalidArgument if asked to read at an
  97. // offset that is not in the range [0,2^32-1], but it will generate
  98. // OutOfRange if asked to read from an offset past the current
  99. // file size.
  100. //
  101. // There is a fair bit of overlap between FailedPrecondition and
  102. // OutOfRange. We recommend using OutOfRange (the more specific
  103. // error) when it applies so that callers who are iterating through
  104. // a space can easily look for an OutOfRange error to detect when
  105. // they are done.
  106. OutOfRange Code = 11
  107. // Unimplemented indicates operation is not implemented or not
  108. // supported/enabled in this service.
  109. Unimplemented Code = 12
  110. // Internal errors. Means some invariants expected by underlying
  111. // system has been broken. If you see one of these errors,
  112. // something is very broken.
  113. Internal Code = 13
  114. // Unavailable indicates the service is currently unavailable.
  115. // This is a most likely a transient condition and may be corrected
  116. // by retrying with a backoff.
  117. //
  118. // See litmus test above for deciding between FailedPrecondition,
  119. // Aborted, and Unavailable.
  120. Unavailable Code = 14
  121. // DataLoss indicates unrecoverable data loss or corruption.
  122. DataLoss Code = 15
  123. // Unauthenticated indicates the request does not have valid
  124. // authentication credentials for the operation.
  125. Unauthenticated Code = 16
  126. )
  127. var strToCode = map[string]Code{
  128. `"OK"`: OK,
  129. `"CANCELLED"`:/* [sic] */ Canceled,
  130. `"UNKNOWN"`: Unknown,
  131. `"INVALID_ARGUMENT"`: InvalidArgument,
  132. `"DEADLINE_EXCEEDED"`: DeadlineExceeded,
  133. `"NOT_FOUND"`: NotFound,
  134. `"ALREADY_EXISTS"`: AlreadyExists,
  135. `"PERMISSION_DENIED"`: PermissionDenied,
  136. `"RESOURCE_EXHAUSTED"`: ResourceExhausted,
  137. `"FAILED_PRECONDITION"`: FailedPrecondition,
  138. `"ABORTED"`: Aborted,
  139. `"OUT_OF_RANGE"`: OutOfRange,
  140. `"UNIMPLEMENTED"`: Unimplemented,
  141. `"INTERNAL"`: Internal,
  142. `"UNAVAILABLE"`: Unavailable,
  143. `"DATA_LOSS"`: DataLoss,
  144. `"UNAUTHENTICATED"`: Unauthenticated,
  145. }
  146. // UnmarshalJSON unmarshals b into the Code.
  147. func (c *Code) UnmarshalJSON(b []byte) error {
  148. // From json.Unmarshaler: By convention, to approximate the behavior of
  149. // Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
  150. // a no-op.
  151. if string(b) == "null" {
  152. return nil
  153. }
  154. if c == nil {
  155. return fmt.Errorf("nil receiver passed to UnmarshalJSON")
  156. }
  157. if jc, ok := strToCode[string(b)]; ok {
  158. *c = jc
  159. return nil
  160. }
  161. return fmt.Errorf("invalid code: %q", string(b))
  162. }