Skip to main content

rspack_cacheable/
error.rs

1use rkyv::rancor::{BoxedError, Source, Trace};
2
3#[derive(Debug)]
4pub enum Error {
5  BoxedError(BoxedError),
6  MessageError(&'static str),
7  DynCheckBytesNotRegister,
8  NoContext,
9  UnsupportedField,
10}
11
12impl std::fmt::Display for Error {
13  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14    match self {
15      Self::BoxedError(error) => error.fmt(f),
16      Self::MessageError(msg) => {
17        write!(f, "{msg}")
18      }
19      Self::DynCheckBytesNotRegister => {
20        write!(f, "cacheable_dyn check bytes not register")
21      }
22      Self::NoContext => {
23        write!(f, "no context")
24      }
25      Self::UnsupportedField => {
26        write!(f, "unsupported field")
27      }
28    }
29  }
30}
31
32impl std::error::Error for Error {
33  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34    match self {
35      Self::BoxedError(error) => error.source(),
36      _ => None,
37    }
38  }
39}
40
41impl Trace for Error {
42  fn trace<R>(self, trace: R) -> Self
43  where
44    R: std::fmt::Debug + std::fmt::Display + Send + Sync + 'static,
45  {
46    Self::BoxedError(BoxedError::trace(BoxedError::new(self), trace))
47  }
48}
49
50impl Source for Error {
51  fn new<T: std::error::Error + Send + Sync + 'static>(source: T) -> Self {
52    Self::BoxedError(BoxedError::new(source))
53  }
54}
55
56pub type Result<T, E = Error> = std::result::Result<T, E>;