use std::fmt;
pub use crate::error::{Error, ErrorContext, Result};
pub use crate::host::log;
#[derive(Debug)]
pub(crate) enum ModuleError {
DuplicateRegistration(String),
}
impl fmt::Display for ModuleError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ModuleError::*;
match self {
DuplicateRegistration(name) => write!(
f,
"WebAssembly module attempted to register 2 different extensions under the same `root_id` \"{}\"",
name,
),
}
}
}
impl std::error::Error for ModuleError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
#[derive(Debug)]
pub(crate) enum ConfigurationError {
UnknownExtension {
requested: String,
available: Vec<String>,
},
}
impl fmt::Display for ConfigurationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ConfigurationError::*;
match self {
UnknownExtension { requested, available } => write!(
f,
"WebAssembly module has no extension with `root_id` \"{}\"; valid `root_id` values are: {:?}",
requested, available
),
}
}
}
impl std::error::Error for ConfigurationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
pub(crate) trait ErrorSink {
fn observe(&self, context: &str, err: &Error);
}
impl dyn ErrorSink {
pub fn default() -> &'static dyn ErrorSink {
&impls::DefaultErrorSink
}
}
mod impls {
use super::{Error, ErrorSink};
use crate::host::log;
pub(super) struct DefaultErrorSink;
impl ErrorSink for DefaultErrorSink {
fn observe(&self, context: &str, err: &Error) {
log::error!("{}: {}", context, err);
}
}
}