pub use std::error;
pub use super::Error;
pub fn user(description: &str, advice: &str) -> Error {
Error::UserError(description.to_string(), advice.to_string(), None, None)
}
pub fn user_with_cause(description: &str, advice: &str, cause: Error) -> Error {
Error::UserError(
description.to_string(),
advice.to_string(),
Some(Box::from(cause)),
None,
)
}
pub fn user_with_internal<T>(description: &str, advice: &str, internal: T) -> Error
where
T: Into<Box<dyn error::Error + Send + Sync>>,
{
Error::UserError(
description.to_string(),
advice.to_string(),
None,
Some(internal.into()),
)
}
pub fn system(description: &str, advice: &str) -> Error {
Error::SystemError(description.to_string(), advice.to_string(), None, None)
}
pub fn system_with_cause(description: &str, advice: &str, cause: Error) -> Error {
Error::SystemError(
description.to_string(),
advice.to_string(),
Some(Box::from(cause)),
None,
)
}
pub fn system_with_internal<T>(description: &str, advice: &str, internal: T) -> Error
where
T: Into<Box<dyn error::Error + Send + Sync>>,
{
Error::SystemError(
description.to_string(),
advice.to_string(),
None,
Some(internal.into()),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_description() {
assert_eq!(
user(
"Something bad happened",
"Avoid bad things happening in future"
)
.description(),
"Something bad happened"
);
assert_eq!(
system(
"Something bad happened",
"Avoid bad things happening in future"
)
.description(),
"Something bad happened"
);
}
#[test]
fn test_message_basic() {
assert_eq!(
user(
"Something bad happened.",
"Avoid bad things happening in future"
)
.message(),
"Oh no! Something bad happened.\n\nTo try and fix this, you can:\n - Avoid bad things happening in future"
);
assert_eq!(
system(
"Something bad happened.",
"Avoid bad things happening in future"
)
.message(),
"Whoops! Something bad happened. (This isn't your fault)\n\nTo try and fix this, you can:\n - Avoid bad things happening in future"
);
}
#[test]
fn test_message_cause() {
assert_eq!(
user_with_cause(
"Something bad happened.",
"Avoid bad things happening in future",
user("You got rate limited by GitHub.", "Wait a few minutes and try again.")
)
.message(),
"Oh no! Something bad happened.\n\nThis was caused by:\n - You got rate limited by GitHub.\n\nTo try and fix this, you can:\n - Wait a few minutes and try again.\n - Avoid bad things happening in future"
);
assert_eq!(
system_with_cause(
"Something bad happened.",
"Avoid bad things happening in future",
system("You got rate limited by GitHub.", "Wait a few minutes and try again.")
)
.message(),
"Whoops! Something bad happened. (This isn't your fault)\n\nThis was caused by:\n - You got rate limited by GitHub.\n\nTo try and fix this, you can:\n - Wait a few minutes and try again.\n - Avoid bad things happening in future"
);
}
}