Skip to main content

subspace_core/diagnostics/
error.rs

1use chrono::{DateTime, Utc};
2
3/// Error facts supplied by the error owner, not collected from logs or state.
4#[derive(Clone)]
5pub struct ErrorDetails {
6    pub(super) title: String,
7    pub(super) message: String,
8    pub(super) stage: String,
9    pub(super) code: Option<String>,
10    pub(super) chain: Option<String>,
11    pub(super) time: ErrorTime,
12    pub(super) private_values: Vec<String>,
13}
14
15#[derive(Clone)]
16pub(super) enum ErrorTime {
17    Recorded(DateTime<Utc>),
18    Observed(DateTime<Utc>),
19}
20
21// qual:allow(dry, boilerplate) reason: "Error owners supply optional recorded facts and private removal inputs through a small private-field API, without a new builder dependency or serializable state."
22impl ErrorDetails {
23    /// Legacy errors without a source timestamp are observed now, not backdated.
24    pub fn new(
25        title: impl Into<String>,
26        message: impl Into<String>,
27        stage: impl Into<String>,
28    ) -> Self {
29        Self {
30            title: title.into(),
31            message: message.into(),
32            stage: stage.into(),
33            code: None,
34            chain: None,
35            time: ErrorTime::Observed(Utc::now()),
36            private_values: Vec::new(),
37        }
38    }
39
40    pub fn with_recorded_at(mut self, at: DateTime<Utc>) -> Self {
41        self.time = ErrorTime::Recorded(at);
42        self
43    }
44
45    #[cfg(test)]
46    pub(crate) fn with_observed_at(mut self, at: DateTime<Utc>) -> Self {
47        self.time = ErrorTime::Observed(at);
48        self
49    }
50
51    pub fn with_code(mut self, code: impl Into<String>) -> Self {
52        self.code = Some(code.into());
53        self
54    }
55
56    /// Include the available underlying chain alongside a friendly code.
57    /// Copy and logging apply the same private-value redaction and text bound.
58    pub fn with_chain(mut self, chain: impl Into<String>) -> Self {
59        self.chain = Some(chain.into());
60        self
61    }
62
63    /// Append target/resource/draft values used only for removal from error
64    /// text. These inputs are never rendered, logged or serialized. Matching
65    /// is literal and case-sensitive, with identifier boundaries to preserve
66    /// ordinary words.
67    pub fn with_private_values(mut self, values: impl IntoIterator<Item = String>) -> Self {
68        self.private_values.extend(values);
69        super::redact::normalize_values(&mut self.private_values);
70        self
71    }
72}