codes.go 8.9 KB

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