1use std::fmt::{Display, Formatter};
2
3#[derive(Debug)]
5pub enum Error {
6 AuthorizationFailed,
8
9 InvalidAgentUrl(String),
11
12 UnableToParseResponse,
14
15 UrlDoesNotExist,
17
18 UnknownResponseStatusCode(String),
20
21 InternalServerError(u16, String),
23
24 UnreachableUrl,
26
27 HttpServiceUnavailable,
29
30 CommandNotAvailable(String),
32
33 UnableToParseOutValue(String),
36
37 InvalidOperator(String),
40}
41
42impl std::error::Error for Error {}
43
44pub 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}