1use 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 QuotaExceeded,
10 VolumeFull,
11 Integrity,
12 Unauthorized,
13 Unavailable,
14 ScopeRemoving,
15 SystemFolderProtected,
16 RepositoryIdentityMismatch,
17 ProtocolMismatch,
18 EvictionFailed,
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 evicted_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 evicted_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.evicted_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 Unauthorized | SystemFolderProtected | ScopeRemoving => 403,
61 Conflict | QuotaExceeded | RepositoryIdentityMismatch | EvictionFailed => 409,
62 VolumeFull => 507,
63 Unavailable | ProtocolMismatch => 503,
64 Internal => 500,
65 }
66 }
67}
68impl std::fmt::Display for StorageError {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 write!(f, "{:?}: {}", self.code, self.detail)
71 }
72}
73impl std::error::Error for StorageError {}