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
// Copyright (C) 2023 Andreas Hartmann <hartan@7x.de>
// GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
// SPDX-License-Identifier: GPL-3.0-or-later

//! # CNF Error Types
use thiserror::Error;

pub mod prelude {
    pub use super::CnfError;
    pub use anyhow::{anyhow, Context};

    /// Type alias for errors in this crate
    pub type Result<T> = std::result::Result<T, CnfError>;
}

#[derive(Error, Debug)]
pub enum CnfError {
    #[error("{provider} in {env}: {error}")]
    Provider {
        provider: crate::provider::Provider,
        env: std::sync::Arc<crate::environment::Environment>,
        error: crate::provider::Error,
    },

    /// Configuration file couldn't be read.
    #[error("couldn't read configuration")]
    Config(String),

    /// Executable couldn't be found.
    #[error("command not found: '{0}'")]
    NotFound(String),

    /// Execution environment is unknown.
    #[error("unknown execution environment!")]
    UnknownEnvironment,

    /// Execution environment is illegal.
    #[error("illegal execution environment: {0}")]
    IllegalEnvironment(String),

    /// Execution of a command in some environment failed.
    #[error("executing '{cmd}' in environment '{env}' failed: '{error:?}'")]
    Execution {
        cmd: String,
        env: String,
        error: std::process::Output,
    },

    /// Provider requirements aren't fulfilled (e.g. some binary/package manager is missing)
    #[error("requirement '{requires}' not fulfilled for provider '{provider}' in '{env}'")]
    Requirements {
        requires: String,
        provider: String,
        env: String,
    },

    /// Transparent error from any source
    #[error(transparent)]
    ApplicationError(#[from] anyhow::Error),

    /// Required feature not implemented yet
    #[error("please implement '{0}' first!")]
    NotImplemented(String),

    /// Other error that doesn't fit a category
    #[error("an error occured while {action}: '{error}'")]
    Other { action: String, error: String },
}