From b68b109b7de1d27a334591fdeeda475c808c812a Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Fri, 15 Feb 2019 08:15:57 -0800 Subject: [PATCH] Implement error for remaining errors and the amalgamation error (#184) --- lib/runtime-core/src/error.rs | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index ac14e4fc1..98f0198f4 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -150,6 +150,39 @@ impl PartialEq for RuntimeError { } } +impl std::fmt::Display for RuntimeError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + RuntimeError::IndirectCallSignature { table } => write!( + f, + "Indirect call signature error with Table Index \"{:?}\"", + table + ), + RuntimeError::IndirectCallToNull { table } => { + write!(f, "Indirect call to null with table index \"{:?}\"", table) + } + RuntimeError::IllegalArithmeticOperation => write!(f, "Illegal arithmetic operation"), + RuntimeError::OutOfBoundsAccess { memory, addr } => match addr { + Some(addr) => write!( + f, + "Out-of-bounds access with memory index {:?} and address {}", + memory, addr + ), + None => write!(f, "Out-of-bounds access with memory index {:?}", memory), + }, + RuntimeError::TableOutOfBounds { table } => { + write!(f, "Table out of bounds with table index \"{:?}\"", table) + } + RuntimeError::Unknown { msg } => { + write!(f, "Unknown runtime error with message: \"{}\"", msg) + } + RuntimeError::User { msg } => write!(f, "User runtime error with message: \"{}\"", msg), + } + } +} + +impl std::error::Error for RuntimeError {} + /// This error type is produced by resolving a wasm function /// given its name. /// @@ -218,6 +251,15 @@ impl PartialEq for CallError { } } +impl std::fmt::Display for CallError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + CallError::Resolve(resolve_error) => write!(f, "Call error: {}", resolve_error), + CallError::Runtime(runtime_error) => write!(f, "Call error: {}", runtime_error), + } + } +} + /// This error type is produced when creating something, /// like a `Memory` or a `Table`. #[derive(Debug, Clone)] @@ -311,3 +353,11 @@ impl From for CallError { CallError::Resolve(resolve_err) } } + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}", self) + } +} + +impl std::error::Error for Error {}