steid

@jamesgill /

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