Skip to main content

rama_ttrpc/types/protos/
code.rs

1/// Get from <https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto>
2/// The canonical error codes for Google APIs.
3///
4///
5/// Sometimes multiple error codes may apply.  Services should return
6/// the most specific error code that applies.  For example, prefer
7/// `OUT_OF_RANGE` over `FAILED_PRECONDITION` if both codes apply.
8/// Similarly prefer `NOT_FOUND` or `ALREADY_EXISTS` over `FAILED_PRECONDITION`.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, prost::Enumeration)]
10#[repr(i32)]
11pub enum Code {
12    /// Not an error; returned on success
13    Ok = 0,
14
15    /// The operation was cancelled, typically by the caller.
16    Cancelled = 1,
17
18    /// Unknown error.  For example, this error may be returned when
19    /// a `Status` value received from another address space belongs to
20    /// an error space that is not known in this address space.  Also
21    /// errors raised by APIs that do not return enough error information
22    /// may be converted to this error.
23    Unknown = 2,
24
25    /// The client specified an invalid argument.  Note that this differs
26    /// from `FAILED_PRECONDITION`.  `INVALID_ARGUMENT` indicates arguments
27    /// that are problematic regardless of the state of the system
28    /// (e.g., a malformed file name).
29    InvalidArgument = 3,
30
31    /// The deadline expired before the operation could complete. For operations
32    /// that change the state of the system, this error may be returned
33    /// even if the operation has completed successfully.  For example, a
34    /// successful response from a server could have been delayed long
35    /// enough for the deadline to expire.
36    DeadlineExceeded = 4,
37
38    /// Some requested entity (e.g., file or directory) was not found.
39    ///
40    /// Note to server developers: if a request is denied for an entire class
41    /// of users, such as gradual feature rollout or undocumented whitelist,
42    /// `NOT_FOUND` may be used. If a request is denied for some users within
43    /// a class of users, such as user-based access control, `PERMISSION_DENIED`
44    /// must be used.
45    NotFound = 5,
46
47    /// The entity that a client attempted to create (e.g., file or directory)
48    /// already exists.
49    AlreadyExists = 6,
50
51    /// The caller does not have permission to execute the specified
52    /// operation. `PERMISSION_DENIED` must not be used for rejections
53    /// caused by exhausting some resource (use `RESOURCE_EXHAUSTED`
54    /// instead for those errors). `PERMISSION_DENIED` must not be
55    /// used if the caller can not be identified (use `UNAUTHENTICATED`
56    /// instead for those errors). This error code does not imply the
57    /// request is valid or the requested entity exists or satisfies
58    /// other pre-conditions.
59    PermissionDenied = 7,
60
61    /// The request does not have valid authentication credentials for the
62    /// operation.
63    Unauthenticated = 16,
64
65    /// Some resource has been exhausted, perhaps a per-user quota, or
66    /// perhaps the entire file system is out of space.
67    ResourceExhausted = 8,
68
69    /// The operation was rejected because the system is not in a state
70    /// required for the operation's execution.  For example, the directory
71    /// to be deleted is non-empty, an rmdir operation is applied to
72    /// a non-directory, etc.
73    ///
74    /// Service implementors can use the following guidelines to decide
75    /// between `FAILED_PRECONDITION`, `ABORTED`, and `UNAVAILABLE`:
76    ///   (a) Use `UNAVAILABLE` if the client can retry just the failing call.
77    ///   (b) Use `ABORTED` if the client should retry at a higher level
78    ///       (e.g., when a client-specified test-and-set fails, indicating the
79    ///       client should restart a read-modify-write sequence).
80    ///   (c) Use `FAILED_PRECONDITION` if the client should not retry until
81    ///       the system state has been explicitly fixed.  E.g., if an "rmdir"
82    ///       fails because the directory is non-empty, `FAILED_PRECONDITION`
83    ///       should be returned since the client should not retry unless
84    ///       the files are deleted from the directory.
85    FailedPrecondition = 9,
86
87    /// The operation was aborted, typically due to a concurrency issue such as
88    /// a sequencer check failure or transaction abort.
89    ///
90    /// See the guidelines above for deciding between `FAILED_PRECONDITION`,
91    /// `ABORTED`, and `UNAVAILABLE`.
92    Aborted = 10,
93
94    /// The operation was attempted past the valid range.  E.g., seeking or
95    /// reading past end-of-file.
96    ///
97    /// Unlike `INVALID_ARGUMENT`, this error indicates a problem that may
98    /// be fixed if the system state changes. For example, a 32-bit file
99    /// system will generate `INVALID_ARGUMENT` if asked to read at an
100    /// offset that is not in the range \[0,2^32-1\], but it will generate
101    /// `OUT_OF_RANGE` if asked to read from an offset past the current
102    /// file size.
103    ///
104    /// There is a fair bit of overlap between `FAILED_PRECONDITION` and
105    /// `OUT_OF_RANGE`.  We recommend using `OUT_OF_RANGE` (the more specific
106    /// error) when it applies so that callers who are iterating through
107    /// a space can easily look for an `OUT_OF_RANGE` error to detect when
108    /// they are done.
109    OutOfRange = 11,
110
111    /// The operation is not implemented or is not supported/enabled in this
112    /// service.
113    Unimplemented = 12,
114
115    /// Internal errors.  This means that some invariants expected by the
116    /// underlying system have been broken.  This error code is reserved
117    /// for serious errors.
118    Internal = 13,
119
120    /// The service is currently unavailable.  This is most likely a
121    /// transient condition, which can be corrected by retrying with
122    /// a backoff.
123    ///
124    /// See the guidelines above for deciding between `FAILED_PRECONDITION`,
125    /// `ABORTED`, and `UNAVAILABLE`.
126    Unavailable = 14,
127
128    /// Unrecoverable data loss or corruption.
129    DataLoss = 15,
130}
131
132impl Code {
133    /// String value of the enum field names used in the protobuf definition.
134    ///
135    /// The values are not transformed in any way and thus are considered stable
136    /// (if the protobuf definition does not change) and safe for programmatic use.
137    #[must_use]
138    pub fn as_str_name(&self) -> &'static str {
139        match self {
140            Self::Ok => "OK",
141            Self::Cancelled => "CANCELLED",
142            Self::Unknown => "UNKNOWN",
143            Self::InvalidArgument => "INVALID_ARGUMENT",
144            Self::DeadlineExceeded => "DEADLINE_EXCEEDED",
145            Self::NotFound => "NOT_FOUND",
146            Self::AlreadyExists => "ALREADY_EXISTS",
147            Self::PermissionDenied => "PERMISSION_DENIED",
148            Self::Unauthenticated => "UNAUTHENTICATED",
149            Self::ResourceExhausted => "RESOURCE_EXHAUSTED",
150            Self::FailedPrecondition => "FAILED_PRECONDITION",
151            Self::Aborted => "ABORTED",
152            Self::OutOfRange => "OUT_OF_RANGE",
153            Self::Unimplemented => "UNIMPLEMENTED",
154            Self::Internal => "INTERNAL",
155            Self::Unavailable => "UNAVAILABLE",
156            Self::DataLoss => "DATA_LOSS",
157        }
158    }
159
160    /// Creates an enum from field names used in the protobuf definition.
161    #[must_use]
162    pub fn from_str_name(value: &str) -> Option<Self> {
163        match value {
164            "OK" => Some(Self::Ok),
165            "CANCELLED" => Some(Self::Cancelled),
166            "UNKNOWN" => Some(Self::Unknown),
167            "INVALID_ARGUMENT" => Some(Self::InvalidArgument),
168            "DEADLINE_EXCEEDED" => Some(Self::DeadlineExceeded),
169            "NOT_FOUND" => Some(Self::NotFound),
170            "ALREADY_EXISTS" => Some(Self::AlreadyExists),
171            "PERMISSION_DENIED" => Some(Self::PermissionDenied),
172            "UNAUTHENTICATED" => Some(Self::Unauthenticated),
173            "RESOURCE_EXHAUSTED" => Some(Self::ResourceExhausted),
174            "FAILED_PRECONDITION" => Some(Self::FailedPrecondition),
175            "ABORTED" => Some(Self::Aborted),
176            "OUT_OF_RANGE" => Some(Self::OutOfRange),
177            "UNIMPLEMENTED" => Some(Self::Unimplemented),
178            "INTERNAL" => Some(Self::Internal),
179            "UNAVAILABLE" => Some(Self::Unavailable),
180            "DATA_LOSS" => Some(Self::DataLoss),
181            _ => None,
182        }
183    }
184}
185
186#[cfg(test)]
187mod tests {
188    use super::Code;
189
190    const ALL: [Code; 17] = [
191        Code::Ok,
192        Code::Cancelled,
193        Code::Unknown,
194        Code::InvalidArgument,
195        Code::DeadlineExceeded,
196        Code::NotFound,
197        Code::AlreadyExists,
198        Code::PermissionDenied,
199        Code::Unauthenticated,
200        Code::ResourceExhausted,
201        Code::FailedPrecondition,
202        Code::Aborted,
203        Code::OutOfRange,
204        Code::Unimplemented,
205        Code::Internal,
206        Code::Unavailable,
207        Code::DataLoss,
208    ];
209
210    /// Values and names must match google/rpc/code.proto: the i32 <-> enum <-> name
211    /// roundtrips are what cross the wire in `Status.code`.
212    #[test]
213    fn code_name_and_value_roundtrip() {
214        for code in ALL {
215            assert_eq!(Code::try_from(code as i32).ok(), Some(code));
216            assert_eq!(Code::from_str_name(code.as_str_name()), Some(code));
217            assert!(!code.as_str_name().is_empty());
218        }
219        assert_eq!(
220            Code::Unauthenticated as i32,
221            16,
222            "the one non-contiguous value"
223        );
224        assert_eq!(Code::try_from(17).ok(), None);
225        assert_eq!(Code::from_str_name("NO_SUCH_CODE"), None);
226    }
227}