diff --git a/build/main.rs b/build/main.rs index 8de1a6e..8bd8526 100644 --- a/build/main.rs +++ b/build/main.rs @@ -22,6 +22,7 @@ pub enum Error { FailedToCopy(String), Decoding(elements::Error, String), Encoding(elements::Error), + Packing(utils::PackingError), Optimizer, } @@ -37,6 +38,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: utils::PackingError) -> Self { + Error::Packing(err) + } +} + impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { use Error::*; @@ -46,6 +53,7 @@ impl std::fmt::Display for Error { Decoding(ref err, ref file) => write!(f, "Decoding error ({}). Must be a valid wasm file {}. Pointed wrong file?", err, file), Encoding(ref err) => write!(f, "Encoding error ({}). Almost impossible to happen, no free disk space?", err), Optimizer => write!(f, "Optimization error due to missing export section. Pointed wrong file?"), + Packing(ref e) => write!(f, "Packing failed due to module structure error: {}. Sure used correct libraries for building contracts?", e), } } } @@ -213,7 +221,8 @@ fn main() { utils::optimize(&mut ctor_module, vec![CREATE_SYMBOL]) .unwrap_or_else(|e| die(Error::from(e))) } - let ctor_module = utils::pack_instance(raw_module, ctor_module).expect("Packing failed"); + let ctor_module = utils::pack_instance(raw_module, ctor_module) + .unwrap_or_else(|e| die(Error::from(e))); parity_wasm::serialize_to_file(&path, ctor_module) .unwrap_or_else(|e| die(Error::Encoding(e))) } else { diff --git a/src/lib.rs b/src/lib.rs index 5e3aec5..8753ec7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,5 +24,5 @@ pub use optimizer::{optimize, Error as OptimizerError}; pub use gas::inject_gas_counter; pub use logger::init_log; pub use ext::{externalize, externalize_mem, underscore_funcs, ununderscore_funcs, shrink_unknown_stack}; -pub use pack::pack_instance; +pub use pack::{pack_instance, Error as PackingError}; pub use runtime_type::inject_runtime_type; diff --git a/src/pack.rs b/src/pack.rs index 9fa66f8..1906dbd 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -1,3 +1,4 @@ +use std::fmt; use parity_wasm::elements::{ self, Section, Opcode, DataSegment, InitExpr, Internal, External, ImportCountType, @@ -19,10 +20,24 @@ pub enum Error { InvalidCreateSignature, NoCreateSymbol, InvalidCreateMember, - NoRetImported, NoImportSection, } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + Error::MalformedModule => write!(f, "Module internal references are inconsistent"), + Error::NoTypeSection => write!(f, "No type section in the module"), + Error::NoExportSection => write!(f, "No export section in the module"), + Error::NoCodeSection => write!(f, "No code section inthe module"), + Error::InvalidCreateSignature => write!(f, "Exported symbol `deploy` has invalid signature, should be () -> ()"), + Error::InvalidCreateMember => write!(f, "Exported symbol `deploy` should be a function"), + Error::NoCreateSymbol => write!(f, "No exported `deploy` symbol"), + Error::NoImportSection => write!(f, "No import section in the module"), + } + } +} + /// If module has an exported "_create" function we want to pack it into "constructor". /// `raw_module` is the actual contract code /// `ctor_module` is the constructor which should return `raw_module`