You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

198 lines
7.1 KiB

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