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

fix: improve error msg on calling json on void function #286

Merged
merged 6 commits into from Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ path = "src/croncat.rs"
[[example]]
name = "various_queries"
path = "src/various_queries.rs"

[[example]]
name = "noop"
path = "src/noop.rs"
21 changes: 21 additions & 0 deletions examples/noop-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "simple-contract"
version = "0.1.0"
publish = false
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
near-sdk = "4.1.1"

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
overflow-checks = true
11 changes: 11 additions & 0 deletions examples/noop-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::near_bindgen;

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Noop;

#[near_bindgen]
impl Noop {
pub fn noop() {}
}
Binary file added examples/res/noop_contract.wasm
Binary file not shown.
19 changes: 19 additions & 0 deletions examples/src/noop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// This contract has only one method `noop` which does nothing and returns nothing.
const NOOP_CONTRACT_WASM_FILEPATH: &str = "./examples/res/noop_contract.wasm";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let wasm = std::fs::read(NOOP_CONTRACT_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;

let res = contract.call("noop").transact().await?.json::<()>();

// Ok to error for call with no return value
assert_eq!(
*res.unwrap_err().kind(),
workspaces::error::ErrorKind::DataConversion,
"the function call returned an empty value, which cannot be parsed as JSON"
);
Ok(())
}
16 changes: 15 additions & 1 deletion workspaces/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,21 @@ impl ExecutionFinalResult {
/// the internal state does not meet up with [`serde::de::DeserializeOwned`]'s
/// requirements.
pub fn json<T: serde::de::DeserializeOwned>(self) -> Result<T> {
self.into_result()?.json()
let val = self.into_result()?;
match val.json() {
Err(err) => {
// This catches the case: `EOF while parsing a value at line 1 column 0`
// for a function that doesn't return anything; this is a more descriptive error.
if *err.kind() == ErrorKind::DataConversion && val.value.repr.is_empty() {
return Err(ErrorKind::DataConversion.custom(
"the function call returned an empty value, which cannot be parsed as JSON",
));
}

Err(err)
}
ok => ok,
}
}

/// Deserialize an instance of type `T` from bytes sourced from the execution
Expand Down
Loading