#[derive(Debug, Fail)]
pub enum FaktoryError {
#[fail(display = "request was malformed: {}", desc)]
Malformed {
desc: String,
},
#[fail(display = "an internal server error occurred: {}", msg)]
Internal {
msg: String,
},
#[fail(display = "expected {}, got unexpected response: {}", expected, received)]
BadType {
expected: &'static str,
received: String,
},
#[fail(display = "server sent malformed {} response: {} in {:?}", typed_as, error, bytes)]
BadResponse {
typed_as: &'static str,
error: &'static str,
bytes: Vec<u8>,
},
#[fail(display = "unreachable")]
#[doc(hidden)]
__Nonexhaustive,
}
impl FaktoryError {
pub(crate) fn new(line: String) -> Self {
let mut parts = line.splitn(2, ' ');
let code = parts.next();
let error = parts.next();
if error.is_none() {
return FaktoryError::Internal {
msg: code.unwrap().to_string(),
};
}
let error = error.unwrap().to_string();
match code {
Some("ERR") => FaktoryError::Internal { msg: error },
Some("MALFORMED") => FaktoryError::Malformed { desc: error },
Some(c) => FaktoryError::Internal {
msg: format!("{} {}", c, error),
},
None => FaktoryError::Internal {
msg: "empty error response".to_string(),
},
}
}
}