Skip to content

Commit

Permalink
Improve error handling in Rust (#2029)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil authored Aug 7, 2024
1 parent d9938eb commit 32fd743
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
20 changes: 12 additions & 8 deletions core/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ use std::{
pub extern "C" fn Cairo0ClassHash(class_json_str: *const c_char, hash: *mut c_uchar) {
let class_json = unsafe { CStr::from_ptr(class_json_str) }.to_str().unwrap();
let class: Result<LegacyContractClass, serde_json::Error> = serde_json::from_str(class_json);
if class.is_err() {
return;
}
let class_hash = class.unwrap().class_hash();
if class_hash.is_err() {
return;
}

let class = match class {
Ok(c) => c,
Err(_) => return,
};

let class_hash = match class.class_hash() {
Ok(h) => h,
Err(_) => return,
};

let hash_slice: &mut [u8] = unsafe { slice::from_raw_parts_mut(hash, 32) };
let hash_bytes = class_hash.unwrap().to_bytes_be();
let hash_bytes = class_hash.to_bytes_be();
hash_slice.copy_from_slice(hash_bytes.as_slice());
}
8 changes: 2 additions & 6 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,10 @@ pub extern "C" fn cairoVMExecute(

let trace =
jsonrpc::new_transaction_trace(&txn_and_query_bit.txn, t, &mut txn_state);
if trace.is_err() {
if let Err(e) = trace {
report_error(
reader_handle,
format!(
"failed building txn state diff reason: {:?}",
trace.err().unwrap()
)
.as_str(),
format!("failed building txn state diff reason: {:?}", e).as_str(),
txn_index as i64,
);
return;
Expand Down

0 comments on commit 32fd743

Please sign in to comment.