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

chore: replace expect calls #1220

Merged
merged 18 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 8 additions & 2 deletions near-sdk-macros/src/core_impl/code_generator/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,16 @@ impl AttrSigInfo {
fn deserialize_data(ty: &SerializerType) -> TokenStream2 {
match ty {
SerializerType::JSON => quote! {
::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")
match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
}
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::BorshDeserialize::try_from_slice(&data).expect("Failed to deserialize callback using Borsh")
match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using Borsh"),
}
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,22 @@ impl ImplItemMethodInfo {
let decomposition = self.attr_signature_info.decomposition_pattern();
let serializer_invocation = match self.attr_signature_info.input_serializer {
SerializerType::JSON => quote! {
::near_sdk::serde_json::from_slice(
&::near_sdk::env::input().expect("Expected input since method has arguments.")
).expect("Failed to deserialize input from JSON.")
match ::near_sdk::env::input() {
Some(input) => match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize input from JSON.")
},
None => near_sdk::env::panic_str("Expected input since method has arguments.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please, use fully-qualified syntax here (add :: in the beginning) since this code is generated into the user code and we want to avoid an unexpected aliasing:

Suggested change
None => near_sdk::env::panic_str("Expected input since method has arguments.")
None => ::near_sdk::env::panic_str("Expected input since method has arguments.")

P.S. Apply this change in other places where applicable (in near-sdk-marcos)

};
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::BorshDeserialize::try_from_slice(
&::near_sdk::env::input().expect("Expected input since method has arguments.")
).expect("Failed to deserialize input from Borsh.")
match ::near_sdk::env::input() {
Some(input) => match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize input from Borsh.")
},
None => near_sdk::env::panic_str("Expected input since method has arguments.")
};
},
};
quote! {
Expand Down Expand Up @@ -354,10 +362,16 @@ impl ImplItemMethodInfo {

let value_ser = |result_serializer: &SerializerType| match result_serializer {
SerializerType::JSON => quote! {
let result = ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON.");
let result = match near_sdk::serde_json::to_vec(&result) {
Ok(v) => v,
Err(_) => std::panic::panic_any("Failed to serialize the return value using JSON."),
};
},
SerializerType::Borsh => quote! {
let result = ::near_sdk::borsh::to_vec(&result).expect("Failed to serialize the return value using Borsh.");
let result = match near_sdk::borsh::to_vec(&result) {
Ok(v) => v,
Err(_) => std::panic::panic_any("Failed to serialize the return value using Borsh."),
frol marked this conversation as resolved.
Show resolved Hide resolved
};
},
};

Expand Down
10 changes: 8 additions & 2 deletions near-sdk-macros/src/core_impl/code_generator/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ pub fn generate_serializer(
let constructor = quote! { let __args = #constructor_call; };
let value_ser = match serializer {
SerializerType::JSON => quote! {
::near_sdk::serde_json::to_vec(&__args).expect("Failed to serialize the cross contract args using JSON.")
match near_sdk::serde_json::to_vec(&__args) {
Ok(serialized) => serialized,
Err(_) => near_sdk::env::panic_str("Failed to serialize the cross contract args using JSON."),
}
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::to_vec(&__args).expect("Failed to serialize the cross contract args using Borsh.")
match near_sdk::borsh::to_vec(&__args) {
Ok(serialized) => serialized,
Err(_) => near_sdk::env::panic_str("Failed to serialize the cross contract args using Borsh."),
}
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ pub extern "C" fn method() {
struct Input {
k: u64,
}
let Input { mut k }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { mut k }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut k);
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ pub extern "C" fn method() {
struct Input {
k: u64,
}
let Input { k }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { k }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, k);
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ pub extern "C" fn method() {
struct Input {
k: u64,
}
let Input { k }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { k }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &k);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ pub extern "C" fn method() {
k: u64,
m: Bar,
}
let Input { k, m }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { k, m }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&mut contract, k, m);
::near_sdk::env::state_write(&contract);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ pub extern "C" fn method() {
k: u64,
m: Bar,
}
let Input { k, m }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { k, m }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
let result = Hello::method(&mut contract, k, m);
let result = ::near_sdk::serde_json::to_vec(&result)
.expect("Failed to serialize the return value using JSON.");
let result = match near_sdk::serde_json::to_vec(&result) {
Ok(v) => v,
Err(_) => {
std::panic::panic_any("Failed to serialize the return value using JSON.")
}
};
::near_sdk::env::value_return(&result);
::near_sdk::env::state_write(&contract);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ pub extern "C" fn method() {
k: u64,
m: Bar,
}
let Input { k, m }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from Borsh.");
let Input { k, m }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from Borsh.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
let result = Hello::method(&mut contract, k, m);
let result = ::near_sdk::borsh::to_vec(&result)
.expect("Failed to serialize the return value using Borsh.");
let result = match near_sdk::borsh::to_vec(&result) {
Ok(v) => v,
Err(_) => {
std::panic::panic_any("Failed to serialize the return value using Borsh.")
}
};
::near_sdk::env::value_return(&result);
::near_sdk::env::state_write(&contract);
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ pub extern "C" fn method() {
::near_sdk::env::setup_panic_hook();
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
let result = Hello::method(&contract);
let result = ::near_sdk::serde_json::to_vec(&result)
.expect("Failed to serialize the return value using JSON.");
let result = match near_sdk::serde_json::to_vec(&result) {
Ok(v) => v,
Err(_) => {
std::panic::panic_any("Failed to serialize the return value using JSON.")
}
};
::near_sdk::env::value_return(&result);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@ pub extern "C" fn method() {
struct Input {
y: ::std::string::String,
}
let Input { y }: Input = ::near_sdk::serde_json::from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from JSON.");
let Input { y }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = ::near_sdk::serde_json::from_slice(&data)
.expect("Failed to deserialize callback using JSON");
let mut x: u64 = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let z: ::std::vec::Vec<u8> = ::near_sdk::serde_json::from_slice(&data)
.expect("Failed to deserialize callback using JSON");
let z: ::std::vec::Vec<u8> = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y, z);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@ pub extern "C" fn method() {
struct Input {
y: ::std::string::String,
}
let Input { y }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice(
&::near_sdk::env::input()
.expect("Expected input since method has arguments."),
)
.expect("Failed to deserialize input from Borsh.");
let Input { y }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
near_sdk::env::panic_str("Failed to deserialize input from Borsh.")
}
}
}
None => near_sdk::env::panic_str("Expected input since method has arguments."),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data)
.expect("Failed to deserialize callback using Borsh");
let mut x: u64 = match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using Borsh"),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let z: ::std::vec::Vec<u8> = ::near_sdk::serde_json::from_slice(&data)
.expect("Failed to deserialize callback using JSON");
let z: ::std::vec::Vec<u8> = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y, z);
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ pub extern "C" fn method() {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = ::near_sdk::serde_json::from_slice(&data)
.expect("Failed to deserialize callback using JSON");
let mut x: u64 = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let y: ::std::string::String = ::near_sdk::serde_json::from_slice(&data)
.expect("Failed to deserialize callback using JSON");
let y: ::std::string::String = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y);
}

Loading
Loading