use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("HTTP transport error: {0}")]
Transport(#[from] reqwest::Error),
#[error("RabbitMQ API error {status}: {reason}")]
Api {
status: u16,
reason: String,
},
#[error("resource not found: {0}")]
NotFound(String),
#[error("failed to deserialize response body: {source}")]
Deserialize {
source: serde_json::Error,
body: String,
},
#[error("invalid URL: {0}")]
InvalidUrl(String),
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::Error;
#[test]
fn api_error_displays_status_and_reason() {
let e = Error::Api {
status: 401,
reason: "not authorised".into(),
};
assert_eq!(e.to_string(), "RabbitMQ API error 401: not authorised");
}
#[test]
fn not_found_displays_identifier() {
let e = Error::NotFound("queue 'q1'".into());
assert_eq!(e.to_string(), "resource not found: queue 'q1'");
}
#[test]
fn invalid_url_displays_url() {
let e = Error::InvalidUrl("ht!tp://nope".into());
assert_eq!(e.to_string(), "invalid URL: ht!tp://nope");
}
#[test]
fn deserialize_displays_source_not_body() {
let source = serde_json::from_str::<String>("42").unwrap_err();
let e = Error::Deserialize {
source,
body: "42".into(),
};
let msg = e.to_string();
assert!(
msg.starts_with("failed to deserialize response body: "),
"got: {msg}"
);
}
}