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