1
0
mirror of https://github.com/fluencelabs/marine.git synced 2025-03-31 13:11:03 +00:00

166 lines
5.1 KiB
Rust
Raw Normal View History

2020-04-18 18:27:01 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-04 19:54:23 +03:00
use fce_wit_interfaces::FCEWITInterfacesError;
2020-07-28 19:14:53 +03:00
use fce_wit_parser::WITParserError;
2020-10-01 12:19:38 +03:00
use crate::HostImportError;
2020-06-04 19:54:23 +03:00
use wasmer_wit::errors::InstructionError;
2020-04-18 18:27:01 +03:00
use wasmer_runtime::error::{
2020-04-22 16:33:19 +03:00
CallError, CompileError, CreationError, Error as WasmerError, ResolveError, RuntimeError,
2020-04-18 18:27:01 +03:00
};
2020-04-22 16:25:24 +03:00
use std::error::Error;
2020-04-18 18:27:01 +03:00
#[derive(Debug)]
2020-05-08 20:38:29 +03:00
pub enum FCEError {
2020-04-18 18:27:01 +03:00
/// This error type is produced by Wasmer during resolving a Wasm function.
WasmerResolveError(String),
/// Error related to calling a main Wasm module.
WasmerInvokeError(String),
2020-04-22 16:11:57 +03:00
/// Error that raises during compilation Wasm code by Wasmer.
WasmerCreationError(String),
2020-04-18 18:27:01 +03:00
2020-04-22 16:11:57 +03:00
/// Error that raises during creation of some Wasm objects (like table and memory) by Wasmer.
WasmerCompileError(String),
2020-04-18 18:27:01 +03:00
2020-04-22 16:11:57 +03:00
/// Error that raises on the preparation step.
PrepareError(String),
2020-04-28 05:00:20 +03:00
/// Indicates that there is already a module with such name.
2020-10-21 22:21:16 +03:00
NonUniqueModuleName(String),
2020-04-28 05:00:20 +03:00
/// Returns when there is no module with such name.
NoSuchFunction(String),
/// Returns when there is no module with such name.
2020-07-11 23:04:55 +03:00
NoSuchModule(String),
2020-05-09 00:39:42 +03:00
2020-10-01 12:19:38 +03:00
/// An error occurred when host functions tries to lift IValues from WValues and lowering back.
HostImportError(HostImportError),
2020-06-04 19:06:23 +03:00
/// WIT section parse error.
WITParseError(WITParserError),
2020-06-04 19:06:23 +03:00
/// Incorrect WIT section.
IncorrectWIT(String),
2020-04-18 18:27:01 +03:00
}
2020-05-08 20:38:29 +03:00
impl Error for FCEError {}
2020-04-22 16:11:57 +03:00
2020-05-08 20:38:29 +03:00
impl std::fmt::Display for FCEError {
2020-04-28 14:39:46 +03:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
2020-04-18 18:27:01 +03:00
match self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerResolveError(msg) => write!(f, "WasmerResolveError: {}", msg),
FCEError::WasmerInvokeError(msg) => write!(f, "WasmerInvokeError: {}", msg),
FCEError::WasmerCompileError(msg) => write!(f, "WasmerCompileError: {}", msg),
FCEError::WasmerCreationError(msg) => write!(f, "WasmerCreationError: {}", msg),
FCEError::PrepareError(msg) => {
2020-10-21 22:21:16 +03:00
write!(f, "Prepare error: {}, probably module is malformed", msg)
2020-04-22 16:11:57 +03:00
}
2020-10-21 22:21:16 +03:00
FCEError::NonUniqueModuleName(module_name) => {
write!(f, r#"FCE already has module with name "{}""#, module_name)
2020-05-09 00:39:42 +03:00
}
2020-10-21 22:21:16 +03:00
FCEError::NoSuchFunction(function_name) => write!(
f,
r#"FCE doesn't have a function with name: {}"#,
function_name
),
FCEError::NoSuchModule(err_msg) => write!(f, "{}", err_msg),
2020-10-01 12:19:38 +03:00
FCEError::HostImportError(host_import_error) => write!(f, "{}", host_import_error),
2020-06-04 19:06:23 +03:00
FCEError::WITParseError(err) => write!(f, "{}", err),
FCEError::IncorrectWIT(err_msg) => write!(f, "{}", err_msg),
2020-04-18 18:27:01 +03:00
}
}
}
2020-10-01 12:19:38 +03:00
impl From<HostImportError> for FCEError {
fn from(err: HostImportError) -> Self {
FCEError::HostImportError(err)
}
}
2020-05-08 20:38:29 +03:00
impl From<CreationError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: CreationError) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerCreationError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-05-08 20:38:29 +03:00
impl From<CompileError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: CompileError) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerCompileError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-05-08 20:38:29 +03:00
impl From<parity_wasm::elements::Error> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: parity_wasm::elements::Error) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::PrepareError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-05-08 20:38:29 +03:00
impl From<CallError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: CallError) -> Self {
match err {
2020-05-08 20:38:29 +03:00
CallError::Resolve(err) => FCEError::WasmerResolveError(format!("{}", err)),
CallError::Runtime(err) => FCEError::WasmerInvokeError(format!("{}", err)),
2020-04-18 18:27:01 +03:00
}
}
}
2020-05-08 20:38:29 +03:00
impl From<ResolveError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: ResolveError) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerResolveError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-05-08 20:38:29 +03:00
impl From<RuntimeError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: RuntimeError) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerInvokeError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-05-08 20:38:29 +03:00
impl From<WasmerError> for FCEError {
2020-04-18 18:27:01 +03:00
fn from(err: WasmerError) -> Self {
2020-05-08 20:38:29 +03:00
FCEError::WasmerInvokeError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
impl From<InstructionError> for FCEError {
fn from(err: InstructionError) -> Self {
FCEError::WasmerInvokeError(format!("{}", err))
2020-04-18 18:27:01 +03:00
}
}
2020-06-04 19:06:23 +03:00
impl From<WITParserError> for FCEError {
fn from(err: WITParserError) -> Self {
FCEError::WITParseError(err)
}
}
2020-06-04 19:54:23 +03:00
impl From<FCEWITInterfacesError> for FCEError {
fn from(err: FCEWITInterfacesError) -> Self {
FCEError::IncorrectWIT(format!("{}", err))
}
}
impl From<()> for FCEError {
fn from(_err: ()) -> Self {
2020-06-05 23:12:02 +03:00
FCEError::IncorrectWIT("failed to parse instructions for adapter type".to_string())
2020-06-04 19:54:23 +03:00
}
}