2021-08-14 01:25:31 +00:00
|
|
|
//! Actor errors.
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
use super::actor::AnyValue;
|
2021-09-19 14:53:37 +00:00
|
|
|
use super::language;
|
2021-09-24 11:04:15 +00:00
|
|
|
use super::schemas::protocol as P;
|
2021-07-03 07:03:52 +00:00
|
|
|
|
|
|
|
use preserves::value::NestedValue;
|
2021-07-24 21:22:01 +00:00
|
|
|
use preserves::value::Value;
|
2021-09-19 14:53:37 +00:00
|
|
|
use preserves_schema::Codec;
|
|
|
|
use preserves_schema::ParseError;
|
2021-07-03 07:03:52 +00:00
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
pub type Error = P::Error<AnyValue>;
|
|
|
|
|
2021-07-03 07:03:52 +00:00
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
write!(f, "Error: {}; detail: {:?}", self.message, self.detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 01:25:31 +00:00
|
|
|
/// Construct an [`Error`] with the given `message` and `detail`.
|
|
|
|
///
|
|
|
|
/// When no relevant detail exists, convention is to set `detail` to `false`.
|
2021-09-24 11:04:15 +00:00
|
|
|
pub fn error<Detail>(message: &str, detail: Detail) -> Error where AnyValue: From<Detail> {
|
2021-07-03 07:03:52 +00:00
|
|
|
Error {
|
|
|
|
message: message.to_owned(),
|
2021-09-24 11:04:15 +00:00
|
|
|
detail: AnyValue::from(detail),
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 01:25:31 +00:00
|
|
|
/// Encodes an [`ActorResult`][crate::actor::ActorResult] as an
|
|
|
|
/// [`AnyValue`][crate::actor::AnyValue].
|
|
|
|
///
|
|
|
|
/// Used primarily when attempting to perform an
|
|
|
|
/// [`Activation`][crate::actor::Activation] on an already-terminated
|
|
|
|
/// actor.
|
2021-09-24 11:04:15 +00:00
|
|
|
pub fn encode_error(result: Result<(), Error>) -> AnyValue {
|
2021-07-24 21:22:01 +00:00
|
|
|
match result {
|
|
|
|
Ok(()) => {
|
2021-09-24 11:04:15 +00:00
|
|
|
let mut r = Value::record(AnyValue::symbol("Ok"), 1);
|
|
|
|
r.fields_vec_mut().push(Value::record(AnyValue::symbol("tuple"), 0).finish().wrap());
|
2021-07-24 21:22:01 +00:00
|
|
|
r.finish().wrap()
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-09-24 11:04:15 +00:00
|
|
|
let mut r = Value::record(AnyValue::symbol("Err"), 1);
|
2021-09-19 14:53:37 +00:00
|
|
|
r.fields_vec_mut().push(language().unparse(&e));
|
2021-07-24 21:22:01 +00:00
|
|
|
r.finish().wrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 16:50:55 +00:00
|
|
|
impl<'a> From<&'a str> for Error {
|
|
|
|
fn from(v: &'a str) -> Self {
|
2021-09-24 11:04:15 +00:00
|
|
|
error(v, AnyValue::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(v: std::io::Error) -> Self {
|
2021-09-24 11:04:15 +00:00
|
|
|
error(&format!("{}", v), AnyValue::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError> for Error {
|
|
|
|
fn from(v: ParseError) -> Self {
|
2021-09-24 11:04:15 +00:00
|
|
|
error(&format!("{}", v), AnyValue::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
impl From<preserves::error::Error> for Error {
|
|
|
|
fn from(v: preserves::error::Error) -> Self {
|
2021-09-24 11:04:15 +00:00
|
|
|
error(&format!("{}", v), AnyValue::new(false))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-30 13:25:58 +00:00
|
|
|
|
|
|
|
impl From<Box<dyn std::error::Error>> for Error {
|
|
|
|
fn from(v: Box<dyn std::error::Error>) -> Self {
|
|
|
|
match v.downcast::<Error>() {
|
|
|
|
Ok(e) => *e,
|
|
|
|
Err(v) => error(&format!("{}", v), AnyValue::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for Error {
|
|
|
|
fn from(v: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
|
|
|
|
match v.downcast::<Error>() {
|
|
|
|
Ok(e) => *e,
|
|
|
|
Err(v) => error(&format!("{}", v), AnyValue::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|