Skip to main content

wasmer_package/
error.rs

1use webc::{ContainerError, DetectError};
2
3#[cfg(feature = "authoring")]
4pub use self::authoring::AuthoringError;
5
6/// Errors that may occur while loading a Wasmer package.
7#[derive(Debug, thiserror::Error)]
8#[allow(clippy::result_large_err)]
9#[non_exhaustive]
10pub enum WasmerPackageError {
11    /// An error from building or unpacking a package from its sources.
12    #[cfg(feature = "authoring")]
13    #[error(transparent)]
14    Authoring(#[from] AuthoringError),
15
16    /// Container Error
17    #[error("container error: {0:?}")]
18    ContainerError(#[from] ContainerError),
19
20    /// Detect Error
21    #[error("detect error: {0:?}")]
22    DetectError(#[from] DetectError),
23}
24
25#[cfg(feature = "authoring")]
26mod authoring {
27    use std::path::PathBuf;
28
29    use crate::package::ManifestError;
30
31    use super::WasmerPackageError;
32
33    /// Errors that may occur while building a package from a directory, a
34    /// `wasmer.toml` manifest, or a tarball.
35    #[derive(Debug, thiserror::Error)]
36    #[non_exhaustive]
37    pub enum AuthoringError {
38        /// Unable to create a temporary directory.
39        #[error("Unable to create a temporary directory")]
40        TempDir(#[source] std::io::Error),
41        /// Unable to open a file.
42        #[error("Unable to open \"{}\"", path.display())]
43        FileOpen {
44            /// The file being opened.
45            path: PathBuf,
46            /// The underlying error.
47            #[source]
48            error: std::io::Error,
49        },
50        /// Unable to read a file.
51        #[error("Unable to read \"{}\"", path.display())]
52        FileRead {
53            /// The file being opened.
54            path: PathBuf,
55            /// The underlying error.
56            #[source]
57            error: std::io::Error,
58        },
59
60        /// Generic IO error.
61        #[error("IO Error: {0:?}")]
62        IoError(#[from] std::io::Error),
63
64        /// Unexpected path format
65        #[error("Malformed path format: {0:?}")]
66        MalformedPath(PathBuf),
67
68        /// Unable to extract the tarball.
69        #[error("Unable to extract the tarball")]
70        Tarball(#[source] std::io::Error),
71        /// Unable to deserialize the `wasmer.toml` file.
72        #[error("Unable to deserialize \"{}\"", path.display())]
73        TomlDeserialize {
74            /// The file being deserialized.
75            path: PathBuf,
76            /// The underlying error.
77            #[source]
78            error: toml::de::Error,
79        },
80        /// Unable to deserialize a json file.
81        #[error("Unable to deserialize \"{}\"", path.display())]
82        JsonDeserialize {
83            /// The file being deserialized.
84            path: PathBuf,
85            /// The underlying error.
86            #[source]
87            error: serde_json::Error,
88        },
89        /// Unable to find the `wasmer.toml` file.
90        #[error("Unable to find the \"wasmer.toml\"")]
91        MissingManifest,
92        /// Unable to canonicalize a path.
93        #[error("Unable to get the absolute path for \"{}\"", path.display())]
94        Canonicalize {
95            /// The path being canonicalized.
96            path: PathBuf,
97            /// The underlying error.
98            #[source]
99            error: std::io::Error,
100        },
101        /// Unable to load the `wasmer.toml` manifest.
102        #[error("Unable to load the \"wasmer.toml\" manifest")]
103        Manifest(#[from] ManifestError),
104        /// A manifest validation error.
105        #[error("The manifest is invalid")]
106        Validation(#[from] wasmer_config::package::ValidationError),
107        /// A path in the fs mapping does not exist
108        #[error("Path: \"{}\" does not exist", path.display())]
109        PathNotExists {
110            /// Path entry in fs mapping
111            path: PathBuf,
112        },
113        /// Any error happening when populating the volumes tree map of a package
114        #[error("Volume creation failed: {0:?}")]
115        VolumeCreation(#[from] anyhow::Error),
116
117        /// Error when serializing or deserializing
118        #[error("serde error: {0:?}")]
119        SerdeError(#[from] ciborium::value::Error),
120    }
121
122    // `?` only applies one `From` conversion, so forward the sources that
123    // `AuthoringError` absorbs straight into `WasmerPackageError`.
124    macro_rules! forward_from {
125        ($($source:ty),* $(,)?) => {
126            $(
127                impl From<$source> for WasmerPackageError {
128                    fn from(error: $source) -> Self {
129                        WasmerPackageError::Authoring(error.into())
130                    }
131                }
132            )*
133        };
134    }
135
136    forward_from!(
137        std::io::Error,
138        ManifestError,
139        wasmer_config::package::ValidationError,
140        anyhow::Error,
141        ciborium::value::Error,
142    );
143}