Skip to content

Commit

Permalink
made methods for promises generate by #[near]. pass error argument to…
Browse files Browse the repository at this point in the history
… promise
  • Loading branch information
PolyProgrammist committed May 7, 2024
1 parent 1d2b594 commit c2b397a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/error-handling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Contract {
}

#[persist_on_error]
pub fn inc_persist_on_error(&mut self, is_error: bool) -> Result<u32, MyErrorEnum> {
pub fn inc_persist_on_e(&mut self, is_error: bool) -> Result<u32, MyErrorEnum> {
self.value += 1;
if is_error {
return Err(MyErrorEnum::X);
Expand Down
6 changes: 3 additions & 3 deletions examples/error-handling/tests/test_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn test_factorial() -> anyhow::Result<()> {
assert!(res.is_success());
assert_eq!(get_value(&contract).await.unwrap(), 1);

let res = contract.call("inc_persist_on_error").args_json(::near_sdk::serde_json::json!{{"is_error": false}}).max_gas().transact().await?;
let res = contract.call("inc_persist_on_e").args_json(::near_sdk::serde_json::json!{{"is_error": false}}).max_gas().transact().await?;
assert!(res.is_success());
assert_eq!(get_value(&contract).await.unwrap(), 2);

Expand All @@ -41,10 +41,10 @@ async fn test_factorial() -> anyhow::Result<()> {
assert!(res.is_failure());
assert_eq!(get_value(&contract).await.unwrap(), 4);

let res = contract.call("inc_persist_on_error").args_json(::near_sdk::serde_json::json!{{"is_error": true}}).max_gas().transact().await?;
let res = contract.call("inc_persist_on_e").args_json(::near_sdk::serde_json::json!{{"is_error": true}}).max_gas().transact().await?;
assert!(res.is_failure());
let string_error = format!("{:?}",res.failures()[0].clone().into_result().unwrap_err());
// assert_eq!(string_error, "Error { repr: Custom { kind: Execution, error: ActionError(ActionError { index: Some(0), kind: FunctionCallError(ExecutionError(\"Smart contract panicked: {\\\"error\\\":{\\\"x\\\":5}}\")) }) } }");
assert_eq!(string_error, "Error { repr: Custom { kind: Execution, error: ActionError(ActionError { index: Some(0), kind: FunctionCallError(ExecutionError(\"Smart contract panicked: {\\\"error\\\":\\\"X\\\"}\")) }) } }");

assert_eq!(get_value(&contract).await.unwrap(), 5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl ImplItemMethodInfo {
// __args).expect("Failed to serialize the cross contract args using JSON.")
// } ;

let promise = Contract::ext(::near_sdk::env::current_account_id()).#new_method_name(String::from("passing this error")).as_return();
let promise = Contract::ext(::near_sdk::env::current_account_id()).#new_method_name(err).as_return();

// let x = ::near_sdk::env::promise_create(
// ::near_sdk::env::current_account_id().clone(),
Expand Down
1 change: 1 addition & 0 deletions near-sdk-macros/src/core_impl/info_extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ pub enum ReturnKind {
#[derive(Clone, PartialEq, Eq)]
pub struct StatusResult {
pub result_type: Type,

pub persist_on_error: bool,
}
11 changes: 8 additions & 3 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,17 @@ fn process_impl_block(
item_impl_info.methods.iter().map(|m| &m.attr_signature_info).for_each(|method| {
let new_method_name = quote::format_ident!("{}_error", method.ident);

if let ReturnKind::ResultWithStatus(..) = method.returns.kind {
if let ReturnKind::ResultWithStatus(status) = &method.returns.kind {
let ty = status.result_type.clone();
let another: syn::Type = parse_quote! { <#ty as near_sdk::ResultTypeExtMy>::Error };
eprintln!("another: {}", quote!{#another});

other_code.extend(quote! {
#[near]
impl Contract {
pub fn #new_method_name(&self, error: String) {
::near_sdk::env::panic_str(&error);
pub fn #new_method_name(&self, err: #another) {
let err = ::near_sdk::serde_json::json!{{"error": err}};
::near_sdk::env::panic_str(&err.to_string());
}
}
// #[cfg(target_arch = "wasm32")]
Expand Down
9 changes: 9 additions & 0 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ pub trait MyContractErrorTrait {

pub fn check_trait<T: MyContractErrorTrait>(_: &T) {}

pub trait ResultTypeExtMy {
type Okay;
type Error;
}

impl<T, E> ResultTypeExtMy for Result<T, E> {
type Okay = T;
type Error = E;
}

#[cfg(feature = "legacy")]
pub mod collections;
Expand Down

0 comments on commit c2b397a

Please sign in to comment.