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
//! Domain-layer error types.
use std::io;
use thiserror::Error;
/// Result alias for domain-layer operations.
pub type DomainResult<T> = Result<T, DomainError>;
/// Error type used by domain ports and domain utilities.
#[derive(Debug, Error)]
pub enum DomainError {
/// Filesystem or other IO failure.
#[error("I/O failure while {context}: {source}")]
Io {
/// Short operation context.
context: &'static str,
/// Source error.
#[source]
source: io::Error,
},
/// Requested entity was not found.
#[error("{entity} not found: {id}")]
NotFound {
/// Entity kind.
entity: &'static str,
/// Requested identifier.
id: String,
},
/// Target was ambiguous and matched multiple entities.
#[error("Ambiguous {entity} target '{input}'. Matches: {matches}")]
AmbiguousTarget {
/// Entity kind.
entity: &'static str,
/// User-provided target.
input: String,
/// Comma-separated matching candidates.
matches: String,
},
}
impl DomainError {
/// Build an IO-flavored domain error with a static context string.
pub fn io(context: &'static str, source: io::Error) -> Self {
Self::Io { context, source }
}
/// Build a not-found error for an entity.
pub fn not_found(entity: &'static str, id: impl Into<String>) -> Self {
Self::NotFound {
entity,
id: id.into(),
}
}
/// Build an ambiguity error for an entity target.
pub fn ambiguous_target(entity: &'static str, input: &str, matches: &[String]) -> Self {
Self::AmbiguousTarget {
entity,
input: input.to_string(),
matches: matches.join(", "),
}
}
}
#[cfg(test)]
#[path = "errors_tests.rs"]
mod errors_tests;