Skip to content

Commit af68206

Browse files
authored
Merge pull request #108 from wasmx/error-handling
Better error handling in transformation modules
2 parents cfdb48e + 42d7754 commit af68206

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

libchisel/src/deployer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{ModuleCreator, ModuleError};
2+
use crate::utils::*;
23
use parity_wasm::builder;
34
use parity_wasm::elements::{CustomSection, Module};
45

@@ -65,27 +66,26 @@ fn deployer_code() -> Vec<u8> {
6566
}
6667

6768
/// Returns a module which contains the deployable bytecode as a custom section.
68-
fn create_custom_deployer(payload: &[u8]) -> Module {
69+
fn create_custom_deployer(payload: &[u8]) -> Result<Module, ModuleError> {
6970
// The standard deployer code, which expects a 32 bit little endian as the trailing content
7071
// immediately following the payload, placed in a custom section.
7172
let code = deployer_code();
7273

7374
// This is the pre-written deployer code.
74-
let mut module: Module = parity_wasm::deserialize_buffer(&code).expect("invalid wasm bytecode");
75+
let mut module: Module = parity_wasm::deserialize_buffer(&code)?;
7576

7677
// Re-write memory to pre-allocate enough for code size
7778
let memory_initial = (payload.len() as u32 / 65536) + 1;
7879
let mem_type = parity_wasm::elements::MemoryType::new(memory_initial, None, false);
7980
module
8081
.memory_section_mut()
82+
// This would be an internal error (.e.g the the deployer code above has no memory section)
8183
.expect("failed to get memory section")
8284
.entries_mut()[0] = mem_type;
8385

8486
// Prepare payload (append length).
8587
let mut custom_payload = payload.to_vec();
86-
custom_payload
87-
.write_i32::<LittleEndian>(payload.len() as i32)
88-
.expect("failed to write payload size");
88+
custom_payload.write_i32::<LittleEndian>(payload.len() as i32)?;
8989

9090
// Prepare and append custom section.
9191
let custom = CustomSection::new("deployer".to_string(), custom_payload);
@@ -94,7 +94,7 @@ fn create_custom_deployer(payload: &[u8]) -> Module {
9494
.sections_mut()
9595
.push(parity_wasm::elements::Section::Custom(custom));
9696

97-
module
97+
Ok(module)
9898
}
9999

100100
/// Returns a module which contains the deployable bytecode as a data segment.
@@ -162,7 +162,7 @@ impl<'a> ModuleCreator for Deployer<'a> {
162162
fn create(&self) -> Result<Module, ModuleError> {
163163
let output = match self {
164164
Deployer::Memory(payload) => create_memory_deployer(&payload),
165-
Deployer::CustomSection(payload) => create_custom_deployer(&payload),
165+
Deployer::CustomSection(payload) => create_custom_deployer(&payload)?,
166166
};
167167

168168
Ok(output)

libchisel/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ impl From<String> for ModuleError {
5757
}
5858
}
5959

60+
impl From<std::io::Error> for ModuleError {
61+
fn from(error: std::io::Error) -> Self {
62+
use std::error::Error;
63+
ModuleError::Custom(error.description().to_string())
64+
}
65+
}
66+
6067
impl fmt::Display for ModuleError {
6168
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6269
write!(

0 commit comments

Comments
 (0)