ito_core/errors.rs
1//! Core-layer error types.
2//!
3//! [`CoreError`] is the canonical error type for `ito-core`. All public
4//! functions in this crate return [`CoreResult<T>`] rather than adapter-level
5//! error types. Adapter layers (CLI, web) convert `CoreError` into their own
6//! presentation types (e.g., miette `Report` for rich terminal output).
7
8use std::io;
9
10use ito_domain::errors::DomainError;
11use thiserror::Error;
12
13/// Result alias for core-layer operations.
14pub type CoreResult<T> = Result<T, CoreError>;
15
16/// Canonical error type for the core orchestration layer.
17///
18/// Variants cover the major failure categories encountered by core use-cases.
19/// None of the variants carry presentation logic — that belongs in the adapter.
20#[derive(Debug, Error)]
21pub enum CoreError {
22 /// An error propagated from the domain layer.
23 #[error(transparent)]
24 Domain(#[from] DomainError),
25
26 /// Filesystem or other I/O failure.
27 #[error("{context}: {source}")]
28 Io {
29 /// Short description of the operation that failed.
30 context: String,
31 /// Underlying I/O error.
32 #[source]
33 source: io::Error,
34 },
35
36 /// Input validation failure (bad arguments, constraint violations).
37 #[error("{0}")]
38 Validation(String),
39
40 /// Parse failure (duration strings, JSON, YAML, etc.).
41 #[error("{0}")]
42 Parse(String),
43
44 /// Process execution failure (git, shell commands).
45 #[error("{0}")]
46 Process(String),
47
48 /// SQLite operation failure.
49 #[error("sqlite error: {0}")]
50 Sqlite(String),
51
52 /// An expected asset or resource was not found.
53 #[error("{0}")]
54 NotFound(String),
55
56 /// Serialization or deserialization failure.
57 #[error("{context}: {message}")]
58 Serde {
59 /// Short description of the operation.
60 context: String,
61 /// Error detail.
62 message: String,
63 },
64}
65
66impl CoreError {
67 /// Build an I/O error with context.
68 pub fn io(context: impl Into<String>, source: io::Error) -> Self {
69 Self::Io {
70 context: context.into(),
71 source,
72 }
73 }
74
75 /// Build a validation error.
76 pub fn validation(msg: impl Into<String>) -> Self {
77 Self::Validation(msg.into())
78 }
79
80 /// Build a parse error.
81 pub fn parse(msg: impl Into<String>) -> Self {
82 Self::Parse(msg.into())
83 }
84
85 /// Build a process error.
86 pub fn process(msg: impl Into<String>) -> Self {
87 Self::Process(msg.into())
88 }
89
90 /// Build a not-found error.
91 pub fn not_found(msg: impl Into<String>) -> Self {
92 Self::NotFound(msg.into())
93 }
94
95 /// Build a serde error.
96 pub fn serde(context: impl Into<String>, message: impl Into<String>) -> Self {
97 Self::Serde {
98 context: context.into(),
99 message: message.into(),
100 }
101 }
102
103 /// Build a sqlite error.
104 pub fn sqlite(msg: impl Into<String>) -> Self {
105 Self::Sqlite(msg.into())
106 }
107}