impl From<anyhow::Error> for Trap (#1753)

* From<anyhow::Error> for Trap

* Add TrapReason::Error

* wasmtime: Improve Error to Trap conversion

* Remove Trap::message
This commit is contained in:
Leonardo Yvens
2020-05-29 17:24:12 -03:00
committed by GitHub
parent 8fce8ddefc
commit 0b3b9c298e
11 changed files with 74 additions and 65 deletions

View File

@@ -113,10 +113,10 @@ mod tests {
.unwrap_err()
.downcast::<Trap>()?;
assert!(
trap.message()
.starts_with("wasm trap: out of bounds memory access"),
trap.to_string()
.contains("wasm trap: out of bounds memory access"),
"bad trap message: {:?}",
trap.message()
trap.to_string()
);
}
@@ -140,8 +140,8 @@ mod tests {
.unwrap_err()
.downcast::<Trap>()?;
assert!(trap
.message()
.starts_with("wasm trap: out of bounds memory access"));
.to_string()
.contains("wasm trap: out of bounds memory access"));
}
Ok(())
}

View File

@@ -181,7 +181,7 @@ fn trap_smoke() -> Result<()> {
let store = Store::default();
let f = Func::wrap(&store, || -> Result<(), Trap> { Err(Trap::new("test")) });
let err = f.call(&[]).unwrap_err().downcast::<Trap>()?;
assert_eq!(err.message(), "test");
assert!(err.to_string().contains("test"));
assert!(err.i32_exit_status().is_none());
Ok(())
}
@@ -203,7 +203,7 @@ fn trap_import() -> Result<()> {
.err()
.unwrap()
.downcast::<Trap>()?;
assert_eq!(trap.message(), "foo");
assert!(trap.to_string().contains("foo"));
Ok(())
}
@@ -397,9 +397,8 @@ fn func_write_nothing() -> anyhow::Result<()> {
let ty = FuncType::new(Box::new([]), Box::new([ValType::I32]));
let f = Func::new(&store, ty, |_, _, _| Ok(()));
let err = f.call(&[]).unwrap_err().downcast::<Trap>()?;
assert_eq!(
err.message(),
"function attempted to return an incompatible value"
);
assert!(err
.to_string()
.contains("function attempted to return an incompatible value"));
Ok(())
}

View File

@@ -90,9 +90,8 @@ fn test_returns_incorrect_type() -> Result<()> {
.call(&[])
.expect_err("the execution should fail")
.downcast::<Trap>()?;
assert_eq!(
trap.message(),
"function attempted to return an incompatible value"
);
assert!(trap
.to_string()
.contains("function attempted to return an incompatible value"));
Ok(())
}

View File

@@ -25,7 +25,7 @@ fn test_trap_return() -> Result<()> {
.expect("error calling function")
.downcast::<Trap>()?;
assert_eq!(e.message(), "test 123");
assert!(e.to_string().contains("test 123"));
Ok(())
}
@@ -64,9 +64,9 @@ fn test_trap_trace() -> Result<()> {
assert_eq!(trace[1].func_offset(), 1);
assert_eq!(trace[1].module_offset(), 0x21);
assert!(
e.message().contains("unreachable"),
e.to_string().contains("unreachable"),
"wrong message: {}",
e.message()
e.to_string()
);
Ok(())
@@ -103,7 +103,7 @@ fn test_trap_trace_cb() -> Result<()> {
assert_eq!(trace[0].func_index(), 2);
assert_eq!(trace[1].module_name().unwrap(), "hello_mod");
assert_eq!(trace[1].func_index(), 1);
assert_eq!(e.message(), "cb throw");
assert!(e.to_string().contains("cb throw"));
Ok(())
}
@@ -135,7 +135,7 @@ fn test_trap_stack_overflow() -> Result<()> {
assert_eq!(trace[i].func_index(), 0);
assert_eq!(trace[i].func_name(), Some("run"));
}
assert!(e.message().contains("call stack exhausted"));
assert!(e.to_string().contains("call stack exhausted"));
Ok(())
}
@@ -234,7 +234,11 @@ fn trap_start_function_import() -> Result<()> {
let sig = FuncType::new(Box::new([]), Box::new([]));
let func = Func::new(&store, sig, |_, _, _| Err(Trap::new("user trap")));
let err = Instance::new(&module, &[func.into()]).err().unwrap();
assert_eq!(err.downcast_ref::<Trap>().unwrap().message(), "user trap");
assert!(err
.downcast_ref::<Trap>()
.unwrap()
.to_string()
.contains("user trap"));
Ok(())
}
@@ -373,7 +377,9 @@ fn call_signature_mismatch() -> Result<()> {
.unwrap()
.downcast::<Trap>()
.unwrap();
assert_eq!(err.message(), "wasm trap: indirect call type mismatch");
assert!(err
.to_string()
.contains("wasm trap: indirect call type mismatch"));
Ok(())
}