use thiserror::Error;
use crate::Module;
#[derive(Error, Debug)]
pub enum Error {
#[error("{0} has no entrypoint. Register one, or add a default to the runtime")]
MissingEntrypoint(Module),
#[error("{0} could not be found in global, or module exports")]
ValueNotFound(String),
#[error("{0} is not a function")]
ValueNotCallable(String),
#[error("{0} could not be encoded as a v8 value")]
V8Encoding(String),
#[error("value could not be deserialized: {0}")]
JsonDecode(String),
#[error("{0}")]
Runtime(String),
#[error("Module timed out: {0}")]
Timeout(String),
}
#[macro_use]
mod error_macro {
macro_rules! map_error {
($source_error:path, $impl:expr) => {
impl From<$source_error> for Error {
fn from(e: $source_error) -> Self {
let fmt: &dyn Fn(&$source_error) -> Self = &$impl;
fmt(&e)
}
}
};
}
}
map_error!(std::cell::BorrowMutError, |e| Error::Runtime(e.to_string()));
map_error!(std::io::Error, |e| Error::Runtime(e.to_string()));
map_error!(deno_core::v8::DataError, |e| Error::Runtime(e.to_string()));
map_error!(deno_core::ModuleResolutionError, |e| Error::Runtime(
e.to_string()
));
map_error!(deno_core::serde_json::Error, |e| Error::JsonDecode(
e.to_string()
));
map_error!(deno_core::serde_v8::Error, |e| Error::JsonDecode(
e.to_string()
));
map_error!(deno_core::anyhow::Error, |e| Error::Runtime(e.to_string()));
map_error!(tokio::time::error::Elapsed, |e| {
Error::Timeout(e.to_string())
});
map_error!(deno_core::futures::channel::oneshot::Canceled, |e| {
Error::Timeout(e.to_string())
});