Skip to content

Commit

Permalink
android add execption (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaorongqiang authored Jun 15, 2023
1 parent 6ddda93 commit 8d3bf52
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/components/wallet_mobile/src/android/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(clippy::needless_return)]

use core::fmt::Display;
use jni::sys::jobject;
use jni::JNIEnv;

/// throw exception and return null if res is an Err(..), this behave like `?` but return null pointer.
macro_rules! throw_exception {
($env:expr, $res: expr) => {
match ThrowExceptionImpl($env, $res) {
Ok(t) => t,
Err(null) => return null as _,
}
};
}

///Throw exception if result it's an Err(..) and return Err(null).
pub(super) fn ThrowExceptionImpl<T, E>(
env: JNIEnv,
result: Result<T, E>,
) -> Result<T, jobject>
where
E: Display,
{
match result {
Ok(t) => Ok(t),
Err(e) => {
let exception_occurred = env.exception_occurred().unwrap();
if !exception_occurred.is_null() {
println!("Uncleared Exception.");
env.exception_describe().unwrap();
env.exception_clear().unwrap();
}

env.throw_new("java/lang/Exception", format!("{}", e))
.unwrap();

let null = core::ptr::null_mut() as jobject;

return Err(null);
}
}
}
16 changes: 11 additions & 5 deletions src/components/wallet_mobile/src/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#[macro_use]
mod exception;

mod constructor;
mod evm;
mod transfer;
mod tx_builder;

use crate::rust::types;
use crate::rust::*;
use exception::ThrowExceptionImpl;
use jni::objects::{JClass, JString};
use jni::sys::{jboolean, jbyteArray, jint, jlong, jstring};
use jni::JNIEnv;
use ledger::data_model::AssetTypeCode;
use zei::xfr::structs::ASSET_TYPE_LENGTH;

#[no_mangle]
/// Returns the git commit hash and commit date of the commit this library was built against.
pub extern "system" fn Java_com_findora_JniApi_buildId(
Expand Down Expand Up @@ -288,7 +291,7 @@ pub extern "system" fn Java_com_findora_JniApi_bech32ToBase64(
.expect("Couldn't get java string!")
.into();

let bs = rs_bech32_to_base64(pk.as_str()).unwrap();
let bs = throw_exception!(env, rs_bech32_to_base64(pk.as_str()));
let output = env.new_string(bs).expect("Couldn't create java string!");
**output
}
Expand All @@ -304,7 +307,7 @@ pub extern "system" fn Java_com_findora_JniApi_base64ToBech32(
.expect("Couldn't get java string!")
.into();

let bs = rs_base64_to_bech32(pk.as_str()).unwrap();
let bs = throw_exception!(env, rs_base64_to_bech32(pk.as_str()));
let output = env.new_string(bs).expect("Couldn't create java string!");
**output
}
Expand All @@ -321,7 +324,7 @@ pub extern "system" fn Java_com_findora_JniApi_base64ToBech32(
/// @see {@link module:Findora-Wasm~ClientAssetRecord#from_json_record|ClientAssetRecord.from_json_record} for information about how to construct an asset record object
/// from a JSON result returned from the ledger server.
pub unsafe extern "system" fn Java_com_findora_JniApi_openClientAssetRecord(
_env: JNIEnv,
env: JNIEnv,
_: JClass,
record_ptr: jlong,
owner_memo_ptr: jlong,
Expand All @@ -335,7 +338,10 @@ pub unsafe extern "system" fn Java_com_findora_JniApi_openClientAssetRecord(
Some(memo.clone())
};
let keypair = &*(keypair_ptr as *mut types::XfrKeyPair);
let oar = rs_open_client_asset_record(record, owner_memo, keypair).unwrap();
let oar = throw_exception!(
env,
rs_open_client_asset_record(record, owner_memo, keypair)
);
Box::into_raw(Box::new(types::OpenAssetRecord::from(oar))) as jlong
}

Expand Down

0 comments on commit 8d3bf52

Please sign in to comment.