Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement panic handler #1299

Merged
merged 13 commits into from
May 11, 2022
34 changes: 16 additions & 18 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn instantiate(
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, HackError> {
// deps.api.debug("here we go 🚀");
deps.api.debug("here we go 🚀");

deps.storage.set(
CONFIG_KEY,
Expand All @@ -31,23 +31,6 @@ pub fn instantiate(
})?,
);

// Uncomment your favourite panic case

// panicked at 'Now what?', src/contract.rs:53:5
panic!("Now what?");

// panicked at 'oh no (a = 3)', src/contract.rs:56:5
// let a = 3;
// panic!("oh no (a = {a})");

// panicked at 'attempt to subtract with overflow', src/contract.rs:59:13
// #[allow(arithmetic_overflow)]
// let _ = 5u32 - 8u32;

// panicked at 'no entry found for key', src/contract.rs:62:13
// let map = std::collections::HashMap::<String, String>::new();
// let _ = map["foo"];

// This adds some unrelated event attribute for testing purposes
Ok(Response::new().add_attribute("Let the", "hacking begin"))
}
Expand Down Expand Up @@ -206,7 +189,22 @@ fn do_allocate_large_memory(pages: u32) -> Result<Response, HackError> {
}

fn do_panic() -> Result<Response, HackError> {
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
// Uncomment your favourite panic case

// panicked at 'This page intentionally faulted', src/contract.rs:53:5
panic!("This page intentionally faulted");

// panicked at 'oh no (a = 3)', src/contract.rs:56:5
// let a = 3;
// panic!("oh no (a = {a})");

// panicked at 'attempt to subtract with overflow', src/contract.rs:59:13
// #[allow(arithmetic_overflow)]
// let _ = 5u32 - 8u32;

// panicked at 'no entry found for key', src/contract.rs:62:13
// let map = std::collections::HashMap::<String, String>::new();
// let _ = map["foo"];
}

fn do_user_errors_in_api_calls(api: &dyn Api) -> Result<Response, HackError> {
Expand Down
2 changes: 1 addition & 1 deletion contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ fn execute_panic() {
);
match execute_res.unwrap_err() {
VmError::RuntimeErr { msg, .. } => {
assert_eq!(msg, "Wasmer runtime error: RuntimeError: unreachable")
assert_eq!(msg, "Wasmer runtime error: RuntimeError: Aborted: panicked at 'This page intentionally faulted', src/contract.rs:195:5")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
err => panic!("Unexpected error: {:?}", err),
}
Expand Down