steid

@jamesgill /

Rust3.7 KBCode·Blame·History·Raw
1use crate::domain::{DomainError, repository::RepositoryError};
2
3use super::port::{
4 GitArchiveError, GitImportError, GitMergeError, GitProtocolError, GitQueryError,
5 GitStorageError, 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}
35
36impl From<DomainError> for Error {
37 fn from(error: DomainError) -> Self {
38 Self::Domain(error)
39 }
40}
41
42impl From<RepositoryError> for Error {
43 fn from(error: RepositoryError) -> Self {
44 Self::Repository(error)
45 }
46}
47
48impl From<PasswordError> for Error {
49 fn from(error: PasswordError) -> Self {
50 Self::Password(error)
51 }
52}
53
54impl From<GitStorageError> for Error {
55 fn from(error: GitStorageError) -> Self {
56 Self::GitStorage(error)
57 }
58}
59
60impl From<GitProtocolError> for Error {
61 fn from(error: GitProtocolError) -> Self {
62 Self::GitProtocol(error)
63 }
64}
65
66impl From<GitQueryError> for Error {
67 fn from(error: GitQueryError) -> Self {
68 Self::GitQuery(error)
69 }
70}
71
72impl From<GitArchiveError> for Error {
73 fn from(error: GitArchiveError) -> Self {
74 Self::GitArchive(error)
75 }
76}
77
78impl From<GitImportError> for Error {
79 fn from(error: GitImportError) -> Self {
80 Self::GitImport(error)
81 }
82}
83
84impl From<GitMergeError> for Error {
85 fn from(error: GitMergeError) -> Self {
86 Self::GitMerge(error)
87 }
88}
89
90impl std::fmt::Display for Error {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 match self {
93 Self::Domain(error) => write!(f, "{error}"),
94 Self::Repository(error) => write!(f, "{error}"),
95 Self::Password(error) => write!(f, "{error}"),
96 Self::GitStorage(error) => write!(f, "{error}"),
97 Self::GitProtocol(error) => write!(f, "{error}"),
98 Self::GitQuery(error) => write!(f, "{error}"),
99 Self::GitArchive(error) => write!(f, "{error}"),
100 Self::GitImport(error) => write!(f, "{error}"),
101 Self::GitMerge(error) => write!(f, "{error}"),
102 }
103 }
104}
105
106impl std::error::Error for Error {
107 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
108 match self {
109 Self::Domain(error) => Some(error),
110 Self::Repository(error) => Some(error),
111 Self::Password(error) => Some(error),
112 Self::GitStorage(error) => Some(error),
113 Self::GitProtocol(error) => Some(error),
114 Self::GitQuery(error) => Some(error),
115 Self::GitArchive(error) => Some(error),
116 Self::GitImport(error) => Some(error),
117 Self::GitMerge(error) => Some(error),
118 }
119 }
120}
121
122pub type Result<T> = std::result::Result<T, Error>;