Repository navigation
Expand file tree
/
Copy patherror.rs
More file actions
100 lines (89 loc) · 2.39 KB
/
Copy patherror.rs
File metadata and controls
100 lines (89 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use serde::{Deserialize, Serialize};
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
InvalidArgument,
PreconditionFailed,
NotFound,
AlreadyExists,
NameConflict,
NotDirectory,
IsDirectory,
DirectoryNotEmpty,
PermissionDenied,
ReadOnly,
Unsupported,
Busy,
CapacityExceeded,
Integrity,
Io,
Unavailable,
RemoteRequired,
RemoteUnknown,
Conflict,
StaleBinding,
StaleAuthority,
TargetMoved,
CandidateMoved,
DirtyWorktree,
BranchBusy,
CoordinatorRequired,
MountBusy,
RequestIdConflict,
RecoveryRequired,
NoCommonBase,
AmbiguousBase,
BaseUnavailable,
UnresolvedConflict,
Cancelled,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Error {
pub code: ErrorCode,
pub message: String,
pub retryable: bool,
}
impl Error {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
retryable: false,
}
}
pub fn retryable(mut self) -> Self {
self.retryable = true;
self
}
pub fn invalid(message: impl Into<String>) -> Self {
Self::new(ErrorCode::InvalidArgument, message)
}
pub fn integrity(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Integrity, message)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.code, self.message)
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
let code = match value.kind() {
std::io::ErrorKind::NotFound => ErrorCode::NotFound,
std::io::ErrorKind::AlreadyExists => ErrorCode::AlreadyExists,
std::io::ErrorKind::PermissionDenied => ErrorCode::PermissionDenied,
std::io::ErrorKind::StorageFull => ErrorCode::CapacityExceeded,
_ => ErrorCode::Io,
};
Self::new(code, value.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::integrity(value.to_string())
}
}