Skip to content

Commit

Permalink
use check_trait for standard errors instead of handle_result
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed May 7, 2024
1 parent 1d4ec49 commit b79ece8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions examples/error-handling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ use near_sdk::contract_error;

#[contract_error]
enum MyErrorEnum {X}
impl AsRef<str> for MyErrorEnum {
fn as_ref(&self) -> &str {
match self {
MyErrorEnum::X => "X",
}
}
}

#[contract_error]
struct MyErrorStruct {
x: u32,
}

#[derive(Debug)]
#[contract_error]
#[near(serializers=[json])]
enum AnotherError {X}
impl AsRef<str> for AnotherError {
fn as_ref(&self) -> &str {
"Not enough balance"
match self {
AnotherError::X => "X",
}
}
}

Expand Down Expand Up @@ -53,7 +62,7 @@ impl Contract {
pub fn inc_just_result(&mut self, is_error: bool) -> Result<u32, MyErrorStruct> {
self.value += 1;
if is_error {
return Err(MyErrorStruct {x: 5});
return Err(MyErrorStruct { x: 5 });
} else {
return Ok(self.value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl ItemImplInfo {
for method in &self.methods {
res.extend(method.method_wrapper());
match method.attr_signature_info.returns.kind {
ReturnKind::HandlesResult {..} => {
ReturnKind::ResultWithStatus {..} => {
let error_type = match &method.attr_signature_info.returns.original {
syn::ReturnType::Default => quote! { () },
syn::ReturnType::Type(_, ty) => {
Expand Down

0 comments on commit b79ece8

Please sign in to comment.