codes.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. Note that it is not always safe to retry
  118. // non-idempotent operations.
  119. //
  120. // See litmus test above for deciding between FailedPrecondition,
  121. // Aborted, and Unavailable.
  122. Unavailable Code = 14
  123. // DataLoss indicates unrecoverable data loss or corruption.
  124. DataLoss Code = 15
  125. // Unauthenticated indicates the request does not have valid
  126. // authentication credentials for the operation.
  127. Unauthenticated Code = 16
  128. _maxCode = 17
  129. )
  130. var strToCode = map[string]Code{
  131. `"OK"`: OK,
  132. `"CANCELLED"`:/* [sic] */ Canceled,
  133. `"UNKNOWN"`: Unknown,
  134. `"INVALID_ARGUMENT"`: InvalidArgument,
  135. `"DEADLINE_EXCEEDED"`: DeadlineExceeded,
  136. `"NOT_FOUND"`: NotFound,
  137. `"ALREADY_EXISTS"`: AlreadyExists,
  138. `"PERMISSION_DENIED"`: PermissionDenied,
  139. `"RESOURCE_EXHAUSTED"`: ResourceExhausted,
  140. `"FAILED_PRECONDITION"`: FailedPrecondition,
  141. `"ABORTED"`: Aborted,
  142. `"OUT_OF_RANGE"`: OutOfRange,
  143. `"UNIMPLEMENTED"`: Unimplemented,
  144. `"INTERNAL"`: Internal,
  145. `"UNAVAILABLE"`: Unavailable,
  146. `"DATA_LOSS"`: DataLoss,
  147. `"UNAUTHENTICATED"`: Unauthenticated,
  148. }
  149. // UnmarshalJSON unmarshals b into the Code.
  150. func (c *Code) UnmarshalJSON(b []byte) error {
  151. // From json.Unmarshaler: By convention, to approximate the behavior of
  152. // Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
  153. // a no-op.
  154. if string(b) == "null" {
  155. return nil
  156. }
  157. if c == nil {
  158. return fmt.Errorf("nil receiver passed to UnmarshalJSON")
  159. }
  160. if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
  161. if ci >= _maxCode {
  162. return fmt.Errorf("invalid code: %q", ci)
  163. }
  164. *c = Code(ci)
  165. return nil
  166. }
  167. if jc, ok := strToCode[string(b)]; ok {
  168. *c = jc
  169. return nil
  170. }
  171. return fmt.Errorf("invalid code: %q", string(b))
  172. }