-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ddda93
commit 8d3bf52
Showing
2 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters