microsandbox_utils/
error.rs

1//! `microsandbox_utils::error` is a module containing error utilities for the microsandbox project.
2
3use std::{
4    error::Error,
5    fmt::{self, Display},
6};
7
8use thiserror::Error;
9
10//--------------------------------------------------------------------------------------------------
11// Types
12//--------------------------------------------------------------------------------------------------
13
14/// The result of a microsandbox-utils-related operation.
15pub type MicrosandboxUtilsResult<T> = Result<T, MicrosandboxUtilsError>;
16
17/// An error that occurred during a file system operation.
18#[derive(pretty_error_debug::Debug, Error)]
19pub enum MicrosandboxUtilsError {
20    /// An error that occurred when validating paths
21    #[error("path validation error: {0}")]
22    PathValidation(String),
23
24    /// An error that occurred when resolving a file
25    #[error("file not found at: {0}\nSource: {1}")]
26    FileNotFound(String, String),
27
28    /// An error that occurred when performing an IO operation
29    #[error("io error: {0}")]
30    IoError(#[from] std::io::Error),
31
32    /// An error that occurred during a runtime operation
33    #[error("runtime error: {0}")]
34    Runtime(String),
35
36    /// An error that occurred during a nix operation
37    #[error("nix error: {0}")]
38    NixError(#[from] nix::Error),
39
40    /// Custom error.
41    #[error("Custom error: {0}")]
42    Custom(#[from] AnyError),
43}
44
45/// An error that can represent any error.
46#[derive(Debug)]
47pub struct AnyError {
48    error: anyhow::Error,
49}
50
51//--------------------------------------------------------------------------------------------------
52// Methods
53//--------------------------------------------------------------------------------------------------
54
55impl MicrosandboxUtilsError {
56    /// Creates a new `Err` result.
57    pub fn custom(error: impl Into<anyhow::Error>) -> MicrosandboxUtilsError {
58        MicrosandboxUtilsError::Custom(AnyError {
59            error: error.into(),
60        })
61    }
62}
63
64impl AnyError {
65    /// Downcasts the error to a `T`.
66    pub fn downcast<T>(&self) -> Option<&T>
67    where
68        T: Display + fmt::Debug + Send + Sync + 'static,
69    {
70        self.error.downcast_ref::<T>()
71    }
72}
73
74//--------------------------------------------------------------------------------------------------
75// Functions
76//--------------------------------------------------------------------------------------------------
77
78/// Creates an `Ok` `MicrosandboxUtilsResult`.
79#[allow(non_snake_case)]
80pub fn Ok<T>(value: T) -> MicrosandboxUtilsResult<T> {
81    Result::Ok(value)
82}
83
84//--------------------------------------------------------------------------------------------------
85// Trait Implementations
86//--------------------------------------------------------------------------------------------------
87
88impl PartialEq for AnyError {
89    fn eq(&self, other: &Self) -> bool {
90        self.error.to_string() == other.error.to_string()
91    }
92}
93
94impl Display for AnyError {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        write!(f, "{}", self.error)
97    }
98}
99
100impl Error for AnyError {}