marine_core/
errors.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::HostImportError;
18use crate::misc::PrepareError;
19
20use marine_it_interfaces::MITInterfacesError;
21use marine_it_parser::ITParserError;
22use marine_module_interface::it_interface::ITInterfaceError;
23use marine_wasm_backend_traits::errors::*;
24
25use thiserror::Error as ThisError;
26
27// TODO: refactor errors
28// TODO: add module name to all errors variants
29
30#[derive(Debug, ThisError)]
31pub enum MError {
32    /// Errors related to failed resolving of records.
33    #[error("{0}")]
34    RecordResolveError(String), // TODO: use a proper error type
35
36    /// Errors arisen during creation of a WASI context.
37    #[error(transparent)]
38    WASIPrepareError(#[from] WasiError),
39
40    /// Errors occurred inside marine-module-interface crate.
41    #[error(transparent)]
42    ModuleInterfaceError(#[from] ITInterfaceError),
43
44    /// Error arisen during execution of Wasm modules (especially, interface types).
45    #[error("Execution error: {0}")]
46    ITInstructionError(#[from] wasmer_it::errors::InstructionError),
47
48    /// Error that raises on the preparation step.
49    #[error(transparent)]
50    PrepareError(#[from] PrepareError),
51
52    /// Indicates that there is already a module with such name.
53    #[error("module with name '{0}' already loaded into Marine, please specify another name")]
54    NonUniqueModuleName(String),
55
56    /// Returns when there is no module with such name.
57    #[error("module with name '{0}' doesn't have function with name {1}")]
58    NoSuchFunction(String, String),
59
60    /// Returns when there is no module with such name.
61    #[error("module with name '{0}' isn't loaded into Marine")]
62    NoSuchModule(String),
63
64    /// An error occurred when host functions tries to lift IValues from WValues and lowering back.
65    #[error(transparent)]
66    HostImportError(#[from] HostImportError),
67
68    /// IT section parse error.
69    #[error(transparent)]
70    WITParseError(#[from] ITParserError),
71
72    /// Incorrect IT section.
73    #[error("{0}")]
74    IncorrectWIT(String), // TODO: use a proper error type
75
76    #[error("Wasm backend error: {0}")]
77    WasmBackendError(#[from] WasmBackendError),
78}
79
80impl From<MITInterfacesError> for MError {
81    fn from(err: MITInterfacesError) -> Self {
82        MError::IncorrectWIT(format!("{}", err))
83    }
84}
85
86impl From<ModuleCreationError> for MError {
87    fn from(value: ModuleCreationError) -> Self {
88        Into::<WasmBackendError>::into(value).into()
89    }
90}
91
92impl From<ResolveError> for MError {
93    fn from(value: ResolveError) -> Self {
94        Into::<WasmBackendError>::into(value).into()
95    }
96}
97
98impl From<ImportError> for MError {
99    fn from(value: ImportError) -> Self {
100        Into::<WasmBackendError>::into(value).into()
101    }
102}
103
104impl From<InstantiationError> for MError {
105    fn from(value: InstantiationError) -> Self {
106        Into::<WasmBackendError>::into(value).into()
107    }
108}
109
110impl From<RuntimeError> for MError {
111    fn from(value: RuntimeError) -> Self {
112        Into::<WasmBackendError>::into(value).into()
113    }
114}