codes.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "strconv"
  24. )
  25. // A Code is a status code defined according to the [gRPC documentation].
  26. //
  27. // Only the codes defined as consts in this package are valid codes. Do not use
  28. // other code values. Behavior of other codes is implementation-specific and
  29. // interoperability between implementations is not guaranteed.
  30. //
  31. // [gRPC documentation]: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
  32. type Code uint32
  33. const (
  34. // OK is returned on success.
  35. OK Code = 0
  36. // Canceled indicates the operation was canceled (typically by the caller).
  37. //
  38. // The gRPC framework will generate this error code when cancellation
  39. // is requested.
  40. Canceled Code = 1
  41. // Unknown error. An example of where this error may be returned is
  42. // if a Status value received from another address space belongs to
  43. // an error-space that is not known in this address space. Also
  44. // errors raised by APIs that do not return enough error information
  45. // may be converted to this error.
  46. //
  47. // The gRPC framework will generate this error code in the above two
  48. // mentioned cases.
  49. Unknown Code = 2
  50. // InvalidArgument indicates client specified an invalid argument.
  51. // Note that this differs from FailedPrecondition. It indicates arguments
  52. // that are problematic regardless of the state of the system
  53. // (e.g., a malformed file name).
  54. //
  55. // This error code will not be generated by the gRPC framework.
  56. InvalidArgument Code = 3
  57. // DeadlineExceeded means operation expired before completion.
  58. // For operations that change the state of the system, this error may be
  59. // returned even if the operation has completed successfully. For
  60. // example, a successful response from a server could have been delayed
  61. // long enough for the deadline to expire.
  62. //
  63. // The gRPC framework will generate this error code when the deadline is
  64. // exceeded.
  65. DeadlineExceeded Code = 4
  66. // NotFound means some requested entity (e.g., file or directory) was
  67. // not found.
  68. //
  69. // This error code will not be generated by the gRPC framework.
  70. NotFound Code = 5
  71. // AlreadyExists means an attempt to create an entity failed because one
  72. // already exists.
  73. //
  74. // This error code will not be generated by the gRPC framework.
  75. AlreadyExists Code = 6
  76. // PermissionDenied indicates the caller does not have permission to
  77. // execute the specified operation. It must not be used for rejections
  78. // caused by exhausting some resource (use ResourceExhausted
  79. // instead for those errors). It must not be
  80. // used if the caller cannot be identified (use Unauthenticated
  81. // instead for those errors).
  82. //
  83. // This error code will not be generated by the gRPC core framework,
  84. // but expect authentication middleware to use it.
  85. PermissionDenied Code = 7
  86. // ResourceExhausted indicates some resource has been exhausted, perhaps
  87. // a per-user quota, or perhaps the entire file system is out of space.
  88. //
  89. // This error code will be generated by the gRPC framework in
  90. // out-of-memory and server overload situations, or when a message is
  91. // larger than the configured maximum size.
  92. ResourceExhausted Code = 8
  93. // FailedPrecondition indicates operation was rejected because the
  94. // system is not in a state required for the operation's execution.
  95. // For example, directory to be deleted may be non-empty, an rmdir
  96. // operation is applied to a non-directory, etc.
  97. //
  98. // A litmus test that may help a service implementor in deciding
  99. // between FailedPrecondition, Aborted, and Unavailable:
  100. // (a) Use Unavailable if the client can retry just the failing call.
  101. // (b) Use Aborted if the client should retry at a higher-level
  102. // (e.g., restarting a read-modify-write sequence).
  103. // (c) Use FailedPrecondition if the client should not retry until
  104. // the system state has been explicitly fixed. E.g., if an "rmdir"
  105. // fails because the directory is non-empty, FailedPrecondition
  106. // should be returned since the client should not retry unless
  107. // they have first fixed up the directory by deleting files from it.
  108. // (d) Use FailedPrecondition if the client performs conditional
  109. // REST Get/Update/Delete on a resource and the resource on the
  110. // server does not match the condition. E.g., conflicting
  111. // read-modify-write on the same resource.
  112. //
  113. // This error code will not be generated by the gRPC framework.
  114. FailedPrecondition Code = 9
  115. // Aborted indicates the operation was aborted, typically due to a
  116. // concurrency issue like sequencer check failures, transaction aborts,
  117. // etc.
  118. //
  119. // See litmus test above for deciding between FailedPrecondition,
  120. // Aborted, and Unavailable.
  121. //
  122. // This error code will not be generated by the gRPC framework.
  123. Aborted Code = 10
  124. // OutOfRange means operation was attempted past the valid range.
  125. // E.g., seeking or reading past end of file.
  126. //
  127. // Unlike InvalidArgument, this error indicates a problem that may
  128. // be fixed if the system state changes. For example, a 32-bit file
  129. // system will generate InvalidArgument if asked to read at an
  130. // offset that is not in the range [0,2^32-1], but it will generate
  131. // OutOfRange if asked to read from an offset past the current
  132. // file size.
  133. //
  134. // There is a fair bit of overlap between FailedPrecondition and
  135. // OutOfRange. We recommend using OutOfRange (the more specific
  136. // error) when it applies so that callers who are iterating through
  137. // a space can easily look for an OutOfRange error to detect when
  138. // they are done.
  139. //
  140. // This error code will not be generated by the gRPC framework.
  141. OutOfRange Code = 11
  142. // Unimplemented indicates operation is not implemented or not
  143. // supported/enabled in this service.
  144. //
  145. // This error code will be generated by the gRPC framework. Most
  146. // commonly, you will see this error code when a method implementation
  147. // is missing on the server. It can also be generated for unknown
  148. // compression algorithms or a disagreement as to whether an RPC should
  149. // be streaming.
  150. Unimplemented Code = 12
  151. // Internal errors. Means some invariants expected by underlying
  152. // system has been broken. If you see one of these errors,
  153. // something is very broken.
  154. //
  155. // This error code will be generated by the gRPC framework in several
  156. // internal error conditions.
  157. Internal Code = 13
  158. // Unavailable indicates the service is currently unavailable.
  159. // This is a most likely a transient condition and may be corrected
  160. // by retrying with a backoff. Note that it is not always safe to retry
  161. // non-idempotent operations.
  162. //
  163. // See litmus test above for deciding between FailedPrecondition,
  164. // Aborted, and Unavailable.
  165. //
  166. // This error code will be generated by the gRPC framework during
  167. // abrupt shutdown of a server process or network connection.
  168. Unavailable Code = 14
  169. // DataLoss indicates unrecoverable data loss or corruption.
  170. //
  171. // This error code will not be generated by the gRPC framework.
  172. DataLoss Code = 15
  173. // Unauthenticated indicates the request does not have valid
  174. // authentication credentials for the operation.
  175. //
  176. // The gRPC framework will generate this error code when the
  177. // authentication metadata is invalid or a Credentials callback fails,
  178. // but also expect authentication middleware to generate it.
  179. Unauthenticated Code = 16
  180. _maxCode = 17
  181. )
  182. var strToCode = map[string]Code{
  183. `"OK"`: OK,
  184. `"CANCELLED"`:/* [sic] */ Canceled,
  185. `"UNKNOWN"`: Unknown,
  186. `"INVALID_ARGUMENT"`: InvalidArgument,
  187. `"DEADLINE_EXCEEDED"`: DeadlineExceeded,
  188. `"NOT_FOUND"`: NotFound,
  189. `"ALREADY_EXISTS"`: AlreadyExists,
  190. `"PERMISSION_DENIED"`: PermissionDenied,
  191. `"RESOURCE_EXHAUSTED"`: ResourceExhausted,
  192. `"FAILED_PRECONDITION"`: FailedPrecondition,
  193. `"ABORTED"`: Aborted,
  194. `"OUT_OF_RANGE"`: OutOfRange,
  195. `"UNIMPLEMENTED"`: Unimplemented,
  196. `"INTERNAL"`: Internal,
  197. `"UNAVAILABLE"`: Unavailable,
  198. `"DATA_LOSS"`: DataLoss,
  199. `"UNAUTHENTICATED"`: Unauthenticated,
  200. }
  201. // UnmarshalJSON unmarshals b into the Code.
  202. func (c *Code) UnmarshalJSON(b []byte) error {
  203. // From json.Unmarshaler: By convention, to approximate the behavior of
  204. // Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
  205. // a no-op.
  206. if string(b) == "null" {
  207. return nil
  208. }
  209. if c == nil {
  210. return fmt.Errorf("nil receiver passed to UnmarshalJSON")
  211. }
  212. if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
  213. if ci >= _maxCode {
  214. return fmt.Errorf("invalid code: %q", ci)
  215. }
  216. *c = Code(ci)
  217. return nil
  218. }
  219. if jc, ok := strToCode[string(b)]; ok {
  220. *c = jc
  221. return nil
  222. }
  223. return fmt.Errorf("invalid code: %q", string(b))
  224. }