use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum StorageErrorCode {
InvalidInput,
NotFound,
Conflict,
VolumeFull,
QuotaExceeded,
Integrity,
Unauthorized,
SessionExpired,
Unavailable,
ScopeRemoving,
SystemFolderProtected,
RepositoryIdentityMismatch,
ProtocolMismatch,
Internal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MutationOutcome {
NotApplied,
Partial,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StorageError {
pub code: StorageErrorCode,
pub detail: String,
pub outcome: MutationOutcome,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub deleted_objects: Option<u64>,
}
impl StorageError {
pub fn new(code: StorageErrorCode, detail: impl Into<String>) -> Self {
Self {
code,
detail: detail.into(),
outcome: MutationOutcome::NotApplied,
deleted_objects: None,
}
}
pub fn uncertain(mut self) -> Self {
self.outcome = MutationOutcome::Unknown;
self
}
pub fn partial(mut self, count: u64) -> Self {
self.outcome = MutationOutcome::Partial;
self.deleted_objects = Some(count);
self
}
pub fn http_status(&self) -> u16 {
use StorageErrorCode::*;
match self.code {
InvalidInput | Integrity => 400,
NotFound => 404,
SessionExpired => 401,
Unauthorized | SystemFolderProtected | ScopeRemoving => 403,
Conflict | RepositoryIdentityMismatch => 409,
VolumeFull | QuotaExceeded => 507,
Unavailable | ProtocolMismatch => 503,
Internal => 500,
}
}
}
impl std::fmt::Display for StorageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}: {}", self.code, self.detail)
}
}
impl std::error::Error for StorageError {}