Skip to main content

postproject_core/
error.rs

1//! Domain-level errors that do not expose backend implementation details.
2
3use crate::TransactionConflict;
4
5/// Stable categories shared by core services and external adapters.
6#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7#[non_exhaustive]
8pub enum ErrorKind {
9    /// A caller supplied invalid input.
10    InvalidArgument,
11    /// A requested domain object does not exist.
12    NotFound,
13    /// An object or relationship already exists.
14    AlreadyExists,
15    /// An operating-system I/O operation failed.
16    Io,
17    /// A persistence backend failed.
18    Storage,
19    /// A schema migration failed.
20    Migration,
21    /// Current state conflicts with the requested operation.
22    Conflict,
23    /// Media resolution produced multiple credible candidates.
24    AmbiguousResolution,
25    /// Media fingerprint calculation or validation failed.
26    Fingerprint,
27    /// The requested operation is not supported.
28    Unsupported,
29    /// The caller cancelled the operation before it completed.
30    Cancelled,
31    /// An internal domain invariant was violated.
32    Internal,
33}
34
35/// An application-neutral error with a stable category and useful context.
36#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
37#[error("{message}")]
38pub struct Error {
39    kind: ErrorKind,
40    message: String,
41    transaction_conflict: Option<Box<TransactionConflict>>,
42}
43
44impl Error {
45    /// Creates an error in `kind` with a human-readable message.
46    #[must_use]
47    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
48        Self {
49            kind,
50            message: message.into(),
51            transaction_conflict: None,
52        }
53    }
54
55    /// Creates a typed optimistic transaction conflict.
56    #[doc(hidden)]
57    #[must_use]
58    pub fn transaction_conflict(conflict: TransactionConflict, message: impl Into<String>) -> Self {
59        Self {
60            kind: ErrorKind::Conflict,
61            message: message.into(),
62            transaction_conflict: Some(Box::new(conflict)),
63        }
64    }
65
66    /// Returns the stable error category.
67    #[must_use]
68    pub const fn kind(&self) -> ErrorKind {
69        self.kind
70    }
71
72    /// Returns the human-readable context.
73    #[must_use]
74    pub fn message(&self) -> &str {
75        &self.message
76    }
77
78    /// Returns machine-readable optimistic-conflict details, when present.
79    #[must_use]
80    pub fn transaction_conflict_detail(&self) -> Option<&TransactionConflict> {
81        self.transaction_conflict.as_deref()
82    }
83}
84
85/// The result type returned by core domain operations.
86pub type Result<T> = std::result::Result<T, Error>;