steid

@jamesgill /

Rust4.3 KBCode·Blame·History·Raw
1use crate::domain::{DomainError, repository::RepositoryError};
2
3use super::port::{
4 BackupError, GitArchiveError, GitImportError, GitMergeError, GitProtocolError, GitQueryError,
5 GitStorageError, MailError, PasswordError,
6};
7
8/// What a use case can fail with.
9///
10/// Keeps the failure sources distinct: a broken rule, broken storage, a broken hash,
11/// and a filesystem that would not cooperate are different problems with different
12/// responses, and flattening them makes a storage outage indistinguishable from a
13/// validation error.
14#[derive(Debug)]
15pub enum Error {
16 /// A domain rule was violated.
17 Domain(DomainError),
18 /// Persistence failed.
19 Repository(RepositoryError),
20 /// Hashing or verification failed for a reason other than a wrong password.
21 Password(PasswordError),
22 /// A bare repository could not be created or removed on disk.
23 GitStorage(GitStorageError),
24 /// The git protocol could not be served.
25 GitProtocol(GitProtocolError),
26 /// A repository's contents could not be read.
27 GitQuery(GitQueryError),
28 /// A repository could not be packed into an archive.
29 GitArchive(GitArchiveError),
30 /// A repository could not be copied in from elsewhere.
31 GitImport(GitImportError),
32 /// A merge could not be written into a repository.
33 GitMerge(GitMergeError),
34 /// An instance backup could not be written, read or restored.
35 Backup(BackupError),
36 /// Mail could not be sent.
37 Mail(MailError),
38}
39
40impl From<DomainError> for Error {
41 fn from(error: DomainError) -> Self {
42 Self::Domain(error)
43 }
44}
45
46impl From<RepositoryError> for Error {
47 fn from(error: RepositoryError) -> Self {
48 Self::Repository(error)
49 }
50}
51
52impl From<PasswordError> for Error {
53 fn from(error: PasswordError) -> Self {
54 Self::Password(error)
55 }
56}
57
58impl From<GitStorageError> for Error {
59 fn from(error: GitStorageError) -> Self {
60 Self::GitStorage(error)
61 }
62}
63
64impl From<GitProtocolError> for Error {
65 fn from(error: GitProtocolError) -> Self {
66 Self::GitProtocol(error)
67 }
68}
69
70impl From<GitQueryError> for Error {
71 fn from(error: GitQueryError) -> Self {
72 Self::GitQuery(error)
73 }
74}
75
76impl From<GitArchiveError> for Error {
77 fn from(error: GitArchiveError) -> Self {
78 Self::GitArchive(error)
79 }
80}
81
82impl From<GitImportError> for Error {
83 fn from(error: GitImportError) -> Self {
84 Self::GitImport(error)
85 }
86}
87
88impl From<GitMergeError> for Error {
89 fn from(error: GitMergeError) -> Self {
90 Self::GitMerge(error)
91 }
92}
93
94impl From<BackupError> for Error {
95 fn from(error: BackupError) -> Self {
96 Self::Backup(error)
97 }
98}
99
100impl From<MailError> for Error {
101 fn from(error: MailError) -> Self {
102 Self::Mail(error)
103 }
104}
105
106impl std::fmt::Display for Error {
107 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108 match self {
109 Self::Domain(error) => write!(f, "{error}"),
110 Self::Repository(error) => write!(f, "{error}"),
111 Self::Password(error) => write!(f, "{error}"),
112 Self::GitStorage(error) => write!(f, "{error}"),
113 Self::GitProtocol(error) => write!(f, "{error}"),
114 Self::GitQuery(error) => write!(f, "{error}"),
115 Self::GitArchive(error) => write!(f, "{error}"),
116 Self::GitImport(error) => write!(f, "{error}"),
117 Self::GitMerge(error) => write!(f, "{error}"),
118 Self::Backup(error) => write!(f, "{error}"),
119 Self::Mail(error) => write!(f, "{error}"),
120 }
121 }
122}
123
124impl std::error::Error for Error {
125 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126 match self {
127 Self::Domain(error) => Some(error),
128 Self::Repository(error) => Some(error),
129 Self::Password(error) => Some(error),
130 Self::GitStorage(error) => Some(error),
131 Self::GitProtocol(error) => Some(error),
132 Self::GitQuery(error) => Some(error),
133 Self::GitArchive(error) => Some(error),
134 Self::GitImport(error) => Some(error),
135 Self::GitMerge(error) => Some(error),
136 Self::Backup(error) => Some(error),
137 Self::Mail(error) => Some(error),
138 }
139 }
140}
141
142pub type Result<T> = std::result::Result<T, Error>;