Skip to main content

storage_api/
error.rs

1//! Stable failures; callers never infer behavior from diagnostic text.
2use serde::{Deserialize, Serialize};
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
5pub enum StorageErrorCode {
6    InvalidInput,
7    NotFound,
8    Conflict,
9    VolumeFull,
10    Integrity,
11    Unauthorized,
12    /// The data-plane credential can be refreshed; no object mutation was attempted.
13    SessionExpired,
14    Unavailable,
15    ScopeRemoving,
16    SystemFolderProtected,
17    RepositoryIdentityMismatch,
18    ProtocolMismatch,
19    Internal,
20}
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub enum MutationOutcome {
24    NotApplied,
25    Partial,
26    Unknown,
27}
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct StorageError {
31    pub code: StorageErrorCode,
32    pub detail: String,
33    pub outcome: MutationOutcome,
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub deleted_objects: Option<u64>,
36}
37impl StorageError {
38    pub fn new(code: StorageErrorCode, detail: impl Into<String>) -> Self {
39        Self {
40            code,
41            detail: detail.into(),
42            outcome: MutationOutcome::NotApplied,
43            deleted_objects: None,
44        }
45    }
46    pub fn uncertain(mut self) -> Self {
47        self.outcome = MutationOutcome::Unknown;
48        self
49    }
50    pub fn partial(mut self, count: u64) -> Self {
51        self.outcome = MutationOutcome::Partial;
52        self.deleted_objects = Some(count);
53        self
54    }
55    pub fn http_status(&self) -> u16 {
56        use StorageErrorCode::*;
57        match self.code {
58            InvalidInput | Integrity => 400,
59            NotFound => 404,
60            SessionExpired => 401,
61            Unauthorized | SystemFolderProtected | ScopeRemoving => 403,
62            Conflict | RepositoryIdentityMismatch => 409,
63            VolumeFull => 507,
64            Unavailable | ProtocolMismatch => 503,
65            Internal => 500,
66        }
67    }
68}
69impl std::fmt::Display for StorageError {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        write!(f, "{:?}: {}", self.code, self.detail)
72    }
73}
74impl std::error::Error for StorageError {}