archunit/common/error/
technical_error.rs

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
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::error::Error;

type BoxedSource = Box<dyn Error + Send + Sync + 'static>;

/// A failure in ArchUnit or its execution environment.
///
/// Examples include an unreadable project, an unavailable Cargo command, and an extraction failure.
/// A rule violation is not a technical error.
#[derive(Debug, thiserror::Error)]
#[error("archunit: {message}{source_suffix}", source_suffix = source_suffix(.source.as_deref()))]
pub struct TechnicalError {
    message: String,
    #[source]
    source: Option<BoxedSource>,
}

impl TechnicalError {
    /// Creates a technical failure diagnosed entirely by ArchUnit.
    #[must_use]
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            source: None,
        }
    }

    /// Adds the lower-level failure that prevented the operation from completing.
    pub fn with_source(
        message: impl Into<String>,
        source: impl Error + Send + Sync + 'static,
    ) -> Self {
        Self {
            message: message.into(),
            source: Some(Box::new(source)),
        }
    }

    /// Returns ArchUnit's stable context without the lower-level source message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

fn source_suffix(source: Option<&(dyn Error + Send + Sync + 'static)>) -> String {
    source.map_or_else(String::new, |source| format!(": {source}"))
}

#[cfg(test)]
mod tests {
    use std::{error::Error, io};

    use super::TechnicalError;

    #[test]
    fn renders_archunit_context_without_a_source() {
        let error = TechnicalError::new("could not locate a Cargo project");

        assert_eq!(error.message(), "could not locate a Cargo project");
        assert_eq!(
            error.to_string(),
            "archunit: could not locate a Cargo project"
        );
        assert!(error.source().is_none());
    }

    #[test]
    fn exposes_the_original_technical_source() {
        let error = TechnicalError::with_source(
            "could not read Cargo.toml",
            io::Error::new(io::ErrorKind::PermissionDenied, "access denied"),
        );

        assert_eq!(
            error.to_string(),
            "archunit: could not read Cargo.toml: access denied"
        );
        assert_eq!(
            error.source().map(ToString::to_string),
            Some("access denied".to_owned())
        );
    }
}