siera_agent/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// User-level errors that can be thrown at runtime
4#[derive(Debug)]
5pub enum Error {
6    /// The cloudagent did not allow the request without proper authorization
7    AuthorizationFailed,
8
9    /// The specified environment does not exist inside the configuration file
10    InvalidAgentUrl(String),
11
12    /// The server gave a response that was not expected and therefore not deserializeable
13    UnableToParseResponse,
14
15    /// Provided url does not exist
16    UrlDoesNotExist,
17
18    /// The server supplied a status code which is not handled accordingly
19    UnknownResponseStatusCode(String),
20
21    /// The server responded with a 5xx status code. Not our fault
22    InternalServerError(u16, String),
23
24    /// Supplied url is not reachable
25    UnreachableUrl,
26
27    /// Specific handle case for a 5xx status code which means that the cloudagent might be offline
28    HttpServiceUnavailable,
29
30    /// Subcommand is not available for this agent
31    CommandNotAvailable(String),
32
33    // TODO: why is this here?
34    /// Predicate structure is invalid
35    UnableToParseOutValue(String),
36
37    // TODO: why is this here?
38    /// Predicate used an invalid operator
39    InvalidOperator(String),
40}
41
42impl std::error::Error for Error {}
43
44/// Generic result type which binds the error to be an instance of the `Error` enum
45pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
46
47impl Display for Error {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        match self {
50            Error::AuthorizationFailed => write!(f, "Failed to authorize. Api-key or authorization token is either wrong or missing."),
51            Error::UnableToParseResponse => write!(f, "Unable to parse the response from the server. Is the cloudagent the correct version?"),
52            Error::UrlDoesNotExist => write!(f, "Path does not exist on agent URL. This can happen when querying by id and the id is not valid or when attempting to use a feature that is not supported on the cloudagent."),
53            Error::UnknownResponseStatusCode(msg) => write!(f, "Received unknown status code from the server. Agent URL is likely incorrect. If the agent URL is correct, please report this error at https://github.com/animo/siera/issues/new Additional info: {msg}"),
54            Error::InternalServerError(status, msg) => write!(f, "Internal Server Error (status code: {status})! Message: {msg}"),
55            Error::UnreachableUrl => write!(f, "Provided url is unreachable. Is the provided agent URL valid?"),
56            Error::HttpServiceUnavailable => write!(f, "Cloudagent is currently unavailable. Are you sure the agent is online?"),
57            Error::UnableToParseOutValue(val) => write!(f, "Unable to parse the predicate values from: {val}. The following structure is required: (name,operator,value)"),
58            Error::InvalidOperator(op) => write!(f, "Invalid Operator ({op}). \">=\", \"<=\", \"=\", \"<\" and \">\" are allowed."),
59            Error::InvalidAgentUrl(url) => write!(f, "Invalid agent url ({url})"),
60            Error::CommandNotAvailable(agent) => write!(f, "Agent '{agent}' does not support this command"),
61        }
62    }
63}