use std::error;
use std::fmt;
use std::time::Duration;
#[derive(Debug)]
pub struct ResourceTemporarilyUnavailableError {
source: Box<dyn error::Error>,
retry_duration_hint: Option<Duration>,
}
impl ResourceTemporarilyUnavailableError {
pub fn from_source(source: Box<dyn error::Error>) -> Self {
Self {
source,
retry_duration_hint: None,
}
}
pub fn from_source_with_hint(
source: Box<dyn error::Error>,
retry_duration_hint: Duration,
) -> Self {
Self {
source,
retry_duration_hint: Some(retry_duration_hint),
}
}
pub fn retry_duration_hint(&self) -> Option<Duration> {
self.retry_duration_hint
}
}
impl error::Error for ResourceTemporarilyUnavailableError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(self.source.as_ref())
}
}
impl fmt::Display for ResourceTemporarilyUnavailableError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.source)
}
}
#[cfg(test)]
pub mod tests {
use std::time::Duration;
use crate::error::InternalError;
use super::*;
#[test]
fn test_display_from_source() {
let msg = "test message";
let err = ResourceTemporarilyUnavailableError::from_source(Box::new(
InternalError::with_message(msg.to_string()),
));
assert_eq!(format!("{}", err), msg);
}
#[test]
fn test_display_from_source_with_hint() {
let msg = "test message";
let err = ResourceTemporarilyUnavailableError::from_source_with_hint(
Box::new(InternalError::with_message(msg.to_string())),
Duration::new(10, 0),
);
assert_eq!(format!("{}", err), msg);
}
}