#[derive(Debug, thiserror::Error)]
pub enum ConnectorError {
#[error("session not found: {0}")]
NotFound(String),
#[error("invalid request: {0}")]
InvalidRequest(String),
#[error("operation not supported by this backend: {0}")]
NotSupported(String),
#[error("transport error: {0}")]
Transport(String),
#[error("backend error: {0}")]
Backend(String),
}
impl ConnectorError {
pub fn is_not_found(&self) -> bool {
matches!(self, ConnectorError::NotFound(_))
}
}
#[cfg(test)]
mod tests {
use super::ConnectorError;
#[test]
fn display_messages_are_stable() {
assert_eq!(
ConnectorError::NotFound("abc".into()).to_string(),
"session not found: abc"
);
assert_eq!(
ConnectorError::InvalidRequest("bad shape".into()).to_string(),
"invalid request: bad shape"
);
assert_eq!(
ConnectorError::NotSupported("delegate".into()).to_string(),
"operation not supported by this backend: delegate"
);
assert_eq!(
ConnectorError::Transport("connection refused".into()).to_string(),
"transport error: connection refused"
);
assert_eq!(
ConnectorError::Backend("rejected".into()).to_string(),
"backend error: rejected"
);
}
#[test]
fn is_not_found_only_matches_not_found_variant() {
assert!(ConnectorError::NotFound("x".into()).is_not_found());
assert!(!ConnectorError::InvalidRequest("x".into()).is_not_found());
assert!(!ConnectorError::NotSupported("x".into()).is_not_found());
assert!(!ConnectorError::Transport("x".into()).is_not_found());
assert!(!ConnectorError::Backend("x".into()).is_not_found());
}
}