|
| 1 | +use core::{any::type_name, fmt}; |
| 2 | + |
| 3 | +#[cfg(doc)] |
| 4 | +use super::{PrunedStore, Store, StoreInner}; |
| 5 | + |
| 6 | +/// An error occurred on [`Store<T>`] or [`StoreInner`] methods. |
| 7 | +#[derive(Debug, Copy, Clone)] |
| 8 | +pub enum StoreError<E> { |
| 9 | + /// An error representing an internal error, a.k.a. a bug or invalid behavior within Wasmi. |
| 10 | + Internal(InternalStoreError), |
| 11 | + /// An external error forwarded by the [`Store`] or [`StoreInner`]. |
| 12 | + External(E), |
| 13 | +} |
| 14 | + |
| 15 | +impl<E: fmt::Display> fmt::Display for StoreError<E> { |
| 16 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 17 | + match self { |
| 18 | + StoreError::Internal(error) => fmt::Display::fmt(error, f), |
| 19 | + StoreError::External(error) => fmt::Display::fmt(error, f), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// A [`Store`] or [`StoreInner`] internal error, a.k.a. bug or invalid behavior within Wasmi. |
| 25 | +#[derive(Debug, Copy, Clone)] |
| 26 | +pub struct InternalStoreError { |
| 27 | + kind: InternalStoreErrorKind, |
| 28 | +} |
| 29 | + |
| 30 | +impl InternalStoreError { |
| 31 | + /// Creates a new [`InternalStoreError`]. |
| 32 | + fn new(kind: InternalStoreErrorKind) -> Self { |
| 33 | + Self { kind } |
| 34 | + } |
| 35 | + |
| 36 | + /// An error indicating that a [`Store`] resource could not be found. |
| 37 | + #[cold] |
| 38 | + #[inline] |
| 39 | + pub fn not_found() -> Self { |
| 40 | + Self::new(InternalStoreErrorKind::EntityNotFound) |
| 41 | + } |
| 42 | + |
| 43 | + /// An error indicating that a [`Store`] resource does not originate from the store. |
| 44 | + #[cold] |
| 45 | + #[inline] |
| 46 | + pub fn store_mismatch() -> Self { |
| 47 | + Self::new(InternalStoreErrorKind::StoreMismatch) |
| 48 | + } |
| 49 | + |
| 50 | + /// An error indicating that restoring a [`PrunedStore`] to a [`Store<T>`] mismatched `T`. |
| 51 | + #[cold] |
| 52 | + #[inline] |
| 53 | + pub fn restore_type_mismatch<T>() -> Self { |
| 54 | + Self::new(InternalStoreErrorKind::RestoreTypeMismatch( |
| 55 | + RestoreTypeMismatchError::new::<T>(), |
| 56 | + )) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl fmt::Display for InternalStoreError { |
| 61 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 62 | + let message = match &self.kind { |
| 63 | + InternalStoreErrorKind::RestoreTypeMismatch(error) => { |
| 64 | + return fmt::Display::fmt(error, f) |
| 65 | + } |
| 66 | + InternalStoreErrorKind::StoreMismatch => "store owner mismatch", |
| 67 | + InternalStoreErrorKind::EntityNotFound => "entity not found", |
| 68 | + }; |
| 69 | + write!(f, "failed to resolve entity: {message}") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Debug, Copy, Clone)] |
| 74 | +enum InternalStoreErrorKind { |
| 75 | + /// An error when restoring a [`PrunedStore`] with an incorrect `T` for [`Store<T>`]. |
| 76 | + RestoreTypeMismatch(RestoreTypeMismatchError), |
| 77 | + /// An error indicating that a store resource does not originate from the given store. |
| 78 | + StoreMismatch, |
| 79 | + /// An error indicating that a store resource was not found. |
| 80 | + EntityNotFound, |
| 81 | +} |
| 82 | + |
| 83 | +impl<E> StoreError<E> { |
| 84 | + /// Create a new [`StoreError`] from the external `error`. |
| 85 | + pub fn external(error: E) -> Self { |
| 86 | + Self::External(error) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<E> From<InternalStoreError> for StoreError<E> { |
| 91 | + fn from(error: InternalStoreError) -> Self { |
| 92 | + Self::Internal(error) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Error occurred when restoring a [`PrunedStore`] to a [`Store<T>`] with an mismatching `T`. |
| 97 | +#[derive(Debug, Copy, Clone)] |
| 98 | +struct RestoreTypeMismatchError { |
| 99 | + type_name: fn() -> &'static str, |
| 100 | +} |
| 101 | + |
| 102 | +impl RestoreTypeMismatchError { |
| 103 | + /// Create a new [`RestoreTypeMismatchError`]. |
| 104 | + pub fn new<T>() -> Self { |
| 105 | + Self { |
| 106 | + type_name: type_name::<T>, |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl fmt::Display for RestoreTypeMismatchError { |
| 112 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 113 | + write!( |
| 114 | + f, |
| 115 | + "failed to unprune store due to type mismatch: {}", |
| 116 | + (self.type_name)() |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
0 commit comments