Skip to main content

gestalt/
rpc_support.rs

1// Code generated by sdkgen. DO NOT EDIT.
2
3//! Shared generated runtime for sdkgen clients: the canonical error model
4//! and the native representation of `google.rpc.Status`.
5
6/// Canonical SDK error codes, drawn from the standard gRPC status codes.
7pub mod gestalt_error_code {
8    /// The operation was cancelled.
9    pub const CANCELLED: i32 = 1;
10    /// The cause of the error is unknown.
11    pub const UNKNOWN: i32 = 2;
12    /// The client specified an invalid argument.
13    pub const INVALID_ARGUMENT: i32 = 3;
14    /// The deadline expired before the operation could complete.
15    pub const DEADLINE_EXCEEDED: i32 = 4;
16    /// The requested entity was not found.
17    pub const NOT_FOUND: i32 = 5;
18    /// The entity the client attempted to create already exists.
19    pub const ALREADY_EXISTS: i32 = 6;
20    /// The caller does not have permission to execute the operation.
21    pub const PERMISSION_DENIED: i32 = 7;
22    /// A resource has been exhausted.
23    pub const RESOURCE_EXHAUSTED: i32 = 8;
24    /// The system is not in a state required for the operation.
25    pub const FAILED_PRECONDITION: i32 = 9;
26    /// The operation was aborted.
27    pub const ABORTED: i32 = 10;
28    /// The operation was attempted past the valid range.
29    pub const OUT_OF_RANGE: i32 = 11;
30    /// The operation is not implemented or supported.
31    pub const UNIMPLEMENTED: i32 = 12;
32    /// An internal error occurred.
33    pub const INTERNAL: i32 = 13;
34    /// The service is currently unavailable.
35    pub const UNAVAILABLE: i32 = 14;
36    /// Unrecoverable data loss or corruption.
37    pub const DATA_LOSS: i32 = 15;
38    /// The request lacks valid authentication credentials.
39    pub const UNAUTHENTICATED: i32 = 16;
40}
41
42/// Canonical SDK error: one numeric gRPC status code, a human-readable
43/// message, and the underlying cause. Transport error types never appear in
44/// public client signatures.
45#[derive(Debug, thiserror::Error)]
46#[error("{message}")]
47pub struct GestaltError {
48    /// Numeric gRPC status code, one of the gestalt_error_code constants.
49    pub code: i32,
50    /// Human-readable error message.
51    pub message: String,
52    // Boxed so the error stays small in every client Result.
53    #[source]
54    source: Option<Box<tonic::Status>>,
55}
56
57impl GestaltError {
58    /// Creates an error with no underlying cause.
59    pub fn new(code: i32, message: impl Into<String>) -> Self {
60        Self {
61            code,
62            message: message.into(),
63            source: None,
64        }
65    }
66}
67
68impl From<tonic::Status> for GestaltError {
69    fn from(status: tonic::Status) -> Self {
70        // tonic reports an expired grpc-timeout as CANCELLED carrying the
71        // TimeoutExpired message; the gRPC spec assigns deadline expiry
72        // DEADLINE_EXCEEDED, so the canonical code is restored here.
73        let timeout_expired = tonic::TimeoutExpired(()).to_string();
74        let code = if status.code() == tonic::Code::Cancelled && status.message() == timeout_expired
75        {
76            gestalt_error_code::DEADLINE_EXCEEDED
77        } else {
78            status.code() as i32
79        };
80        Self {
81            code,
82            message: status.message().to_string(),
83            source: Some(Box::new(status)),
84        }
85    }
86}
87
88/// Native representation of google.rpc.Status carried in response payloads,
89/// mirroring the canonical error model.
90#[derive(Clone, Debug, Default, Eq, PartialEq)]
91pub struct RpcStatus {
92    /// Numeric gRPC status code, one of the gestalt_error_code constants.
93    pub code: i32,
94    /// Human-readable error message.
95    pub message: String,
96}