numcodecs_wasm_host/
error.rs

1use std::sync::Arc;
2
3#[derive(Debug, thiserror::Error)]
4#[error(transparent)]
5/// Opaque error type for errors that occur within the WebAssembly runtime.
6pub struct RuntimeError(#[from] anyhow::Error);
7
8#[derive(Debug, thiserror::Error)]
9#[error("{msg}")]
10/// Opaque error type for errors that occur within the codec implementations
11/// inside the WebAssembly component.
12///
13/// The error preserves the complete causality chain, i.e. calling
14/// [`std::error::Error::source`] works, though the types in the chain are
15/// erased.
16pub struct CodecError {
17    msg: Arc<str>,
18    source: Option<Box<CodecError>>,
19}
20
21impl CodecError {
22    pub(crate) fn new(message: Arc<str>, chain: Vec<Arc<str>>) -> Self {
23        let mut root = Self {
24            msg: message,
25            source: None,
26        };
27
28        let mut err = &mut root;
29
30        for msg in chain {
31            err = &mut *err.source.insert(Box::new(Self { msg, source: None }));
32        }
33
34        root
35    }
36}