pub use super::{Error, Kind};
use std::{borrow::Cow, error};
pub fn user<T>(error: T, advice: &'static [&'static str]) -> Error
where
T: Into<Box<dyn error::Error + Send + Sync>>,
{
Error::new(error.into(), Kind::User, advice)
}
pub fn wrap_user<
S: Into<Cow<'static, str>> + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + 'static,
>(
inner: E,
message: S,
advice: &'static [&'static str],
) -> Error {
Error::new(super::wrap(inner, message), Kind::User, advice)
}
pub fn system<T>(error: T, advice: &'static [&'static str]) -> Error
where
T: Into<Box<dyn error::Error + Send + Sync>>,
{
Error::new(error.into(), Kind::System, advice)
}
pub fn wrap_system<
S: Into<Cow<'static, str>> + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + 'static,
>(
inner: E,
message: S,
advice: &'static [&'static str],
) -> Error {
Error::new(super::wrap(inner, message), Kind::System, advice)
}
#[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_wrapped() {
assert_eq!(
wrap_user(
"You got rate limited",
"Something bad happened.",
&["Avoid bad things happening in future"]
)
.message(),
"Oh no! Something bad happened.\n\nThis was caused by:\n - You got rate limited\n\nTo try and fix this, you can:\n - Avoid bad things happening in future"
);
assert_eq!(
wrap_system(
"You got rate limited",
"Something bad happened.",
&["Avoid bad things happening in future"]
)
.message(),
"Whoops! Something bad happened. (This isn't your fault)\n\nThis was caused by:\n - You got rate limited\n\nTo try and fix this, you can:\n - Avoid bad things happening in future"
);
}
}