scl_lib/controller_utils/
error.rs

1// SPDX-License-Identifier: EUPL-1.2
2use crate::client;
3
4/// General error type for any controller's errors related **only** to
5/// the SCL API and controller's behavior itself.
6#[derive(Debug)]
7pub enum ControllerBasicError {
8    /// Indicates that an error occurred during SCL API interaction.
9    SclClient(client::Error),
10
11    /// Indicates that an error occurred while running a child process
12    /// on the local system.
13    System(String),
14
15    /// Indicates logic problems that should to be fixed.
16    Application,
17
18    /// Indicates that the controller cannot properly work because the
19    /// execution environment is lacking (e.g., cannot find required
20    /// programs).
21    Configuration(String),
22}
23
24impl From<client::Error> for ControllerBasicError {
25    fn from(e: client::Error) -> Self {
26        Self::SclClient(e)
27    }
28}
29
30impl std::fmt::Display for ControllerBasicError {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            ControllerBasicError::SclClient(e) => write!(f, "API error: {}", e),
34            ControllerBasicError::Application => write!(f, "Application error"),
35            ControllerBasicError::System(s) => write!(f, "System error: {}", s),
36            ControllerBasicError::Configuration(s) => write!(f, "Configuration error: {}", s),
37        }
38    }
39}
40
41impl std::error::Error for ControllerBasicError {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            ControllerBasicError::SclClient(e) => Some(e),
45            _ => None,
46        }
47    }
48}