marine/
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 marine_core::MError;
18use marine_wasm_backend_traits::MemoryAllocationStats;
19use it_json_serde::ITJsonSeDeError;
20
21use thiserror::Error;
22
23use std::path::PathBuf;
24
25#[derive(Debug, Error)]
26pub enum MarineError {
27    /// Errors that happened due to invalid config content
28    #[error("InvalidConfig: {0}")]
29    InvalidConfig(String),
30
31    /// An error occurred at the instantiation step.
32    #[error(
33        "module with name {module_import_name} is specified in config (dir: {modules_dir:?}), \
34         but not found in provided modules: {provided_modules:?}"
35    )]
36    InstantiationError {
37        module_import_name: String,
38        modules_dir: Option<PathBuf>,
39        provided_modules: Vec<String>,
40    },
41
42    /// Various errors related to file i/o.
43    #[error("IOError: {0}")]
44    IOError(String),
45
46    /// A function with specified name is missing.
47    #[error("function with name `{0}` is missing")]
48    MissingFunctionError(String),
49
50    /// An argument with specified name is missing.
51    #[error(r#"argument with name "{0}" is missing"#)]
52    MissingArgumentError(String),
53
54    /// Returns when there is no module with such name.
55    #[error(r#"module with name "{0}" is missing"#)]
56    NoSuchModule(String),
57
58    /// Provided arguments aren't compatible with a called function signature.
59    #[error(r#"arguments from json deserialization error in module "{module_name}", function "{function_name}": {error}"#)]
60    JsonArgumentsDeserializationError {
61        module_name: String,
62        function_name: String,
63        error: ITJsonSeDeError,
64    },
65
66    /// Returned outputs aren't compatible with a called function signature.
67    #[error(r#"output to json serialization error in module "{module_name}", function "{function_name}": {error}"#)]
68    JsonOutputSerializationError {
69        module_name: String,
70        function_name: String,
71        error: ITJsonSeDeError,
72    },
73
74    /// Errors related to invalid config.
75    #[error("parsing config error: {0}")]
76    ParseConfigError(#[from] toml::de::Error),
77
78    /// Marine errors.
79    #[error("engine error: {0}")]
80    EngineError(#[from] MError),
81
82    /// When marine returned an error and there was a rejected allocation,
83    /// the most probable cause is OOM. Otherwise this error is the same as EngineError.
84    /// This error is on marine-runtime level,
85    /// because otherwise it is impossible to check allocation stats after a failed instantiation.
86    #[error("Engine error when OOM suspected ({0} failed allocations), original error: {original_error}", .allocation_stats.allocation_rejects)]
87    HighProbabilityOOM {
88        original_error: MError,
89        allocation_stats: MemoryAllocationStats,
90    },
91}
92
93impl From<std::convert::Infallible> for MarineError {
94    fn from(_: std::convert::Infallible) -> Self {
95        unreachable!()
96    }
97}
98
99#[macro_export]
100macro_rules! json_to_marine_err {
101    ($json_expr:expr, $module_name:expr, $function_name:expr) => {
102        $json_expr.map_err(|e| match e {
103            it_json_serde::ITJsonSeDeError::Se(_) => MarineError::JsonOutputSerializationError {
104                module_name: $module_name,
105                function_name: $function_name,
106                error: e,
107            },
108            it_json_serde::ITJsonSeDeError::De(_) => {
109                MarineError::JsonArgumentsDeserializationError {
110                    module_name: $module_name,
111                    function_name: $function_name,
112                    error: e,
113                }
114            }
115        })
116    };
117}