marine_wasm_backend_traits/
errors.rs

1/*
2 * Copyright 2023 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::WType;
18
19use thiserror::Error;
20
21pub type WasmBackendResult<T> = Result<T, WasmBackendError>;
22pub type ResolveResult<T> = Result<T, ResolveError>;
23pub type RuntimeResult<T> = Result<T, RuntimeError>;
24pub type ModuleCreationResult<T> = Result<T, ModuleCreationError>;
25pub type InstantiationResult<T> = Result<T, InstantiationError>;
26pub type WasiResult<T> = Result<T, WasiError>;
27
28/*
29   General error design goals:
30       * expose as much detail as possible
31       * make as much domain-specific errors as possible implementation-independent
32
33   So, Error enums should follow this principle:
34       * errors fully expressible without implementation info should have implementation-independent view
35       * errors not fully expressible without implementation info should have some common view and a way to get implmententation-specific details
36       * "Other" type for all errors not suited for listed options
37*/
38
39#[derive(Debug, Error)]
40pub enum WasmBackendError {
41    #[error(transparent)]
42    ResolveError(#[from] ResolveError),
43
44    #[error(transparent)]
45    RuntimeError(#[from] RuntimeError),
46
47    #[error(transparent)]
48    ModuleCreationError(#[from] ModuleCreationError),
49
50    #[error(transparent)]
51    ImportError(#[from] ImportError),
52
53    #[error(transparent)]
54    InstantiationError(#[from] InstantiationError),
55
56    #[error(transparent)]
57    InitializationError(anyhow::Error),
58}
59
60#[derive(Debug, Error)]
61pub enum ResolveError {
62    #[error("export not found: {0}")]
63    ExportNotFound(String),
64
65    #[error("export type mismatch: expected {expected}, found {actual}")]
66    ExportTypeMismatch {
67        expected: &'static str,
68        actual: &'static str,
69    },
70
71    #[error(transparent)]
72    Other(#[from] anyhow::Error),
73}
74
75#[derive(Debug, Error)]
76pub enum RuntimeError {
77    #[error("Unsupported type encountered: {0}")]
78    UnsupportedType(WType),
79
80    #[error("Trap occurred: {0}")]
81    Trap(anyhow::Error),
82
83    #[error(transparent)]
84    UserError(#[from] UserError),
85
86    #[error("A function returned invalid number of results: expected {expected}, got {actual}")]
87    IncorrectResultsNumber { expected: usize, actual: usize },
88
89    #[error("Unrecognized error: {0}")]
90    Other(anyhow::Error),
91}
92
93#[derive(Debug, Error)]
94pub enum ModuleCreationError {
95    #[error(transparent)]
96    FailedToCompileWasm(anyhow::Error),
97
98    #[error("{0}")]
99    FailedToExtractCustomSections(String), // TODO: use a proper error type
100
101    #[error(transparent)]
102    Other(anyhow::Error),
103}
104
105#[derive(Debug, Error)]
106pub enum ImportError {
107    #[error("Duplicate import")]
108    DuplicateImport(String, String),
109
110    #[error(transparent)]
111    Other(#[from] anyhow::Error),
112}
113
114#[derive(Debug, Error)]
115pub enum InstantiationError {
116    #[error(transparent)]
117    RuntimeError(RuntimeError),
118
119    #[error(transparent)]
120    Other(#[from] anyhow::Error),
121}
122
123#[derive(Debug, Error)]
124pub enum WasiError {
125    #[error(transparent)]
126    IOError(#[from] std::io::Error),
127
128    #[error(transparent)]
129    EngineWasiError(#[from] anyhow::Error),
130
131    #[error("Cumulative size of args array exceeds 2^32")]
132    TooLargeArgsArray,
133
134    #[error("Cumulative size of envs array exceeds 2^32")]
135    TooLargeEnvsArray,
136}
137
138#[derive(Debug, Error)]
139pub enum UserError {
140    #[error(transparent)]
141    Recoverable(anyhow::Error),
142
143    #[error(transparent)]
144    Unrecoverable(anyhow::Error),
145}