diff --git a/objc2/src/exception.rs b/objc2/src/exception.rs index 50013c70c..2465e3aa0 100644 --- a/objc2/src/exception.rs +++ b/objc2/src/exception.rs @@ -1,8 +1,8 @@ use core::ptr::NonNull; -use crate::rc::Id; +use crate::rc::{Id, Shared}; use crate::runtime::Object; -use objc2_exception::{r#try, Exception}; +use objc2_exception::r#try; // Comment copied from `objc2_exception` @@ -21,6 +21,8 @@ use objc2_exception::{r#try, Exception}; /// undefined behaviour until `C-unwind` is stabilized, see [RFC-2945]. /// /// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html -pub unsafe fn catch_exception(closure: impl FnOnce() -> R) -> Result> { - r#try(closure).map_err(|e| Id::new(NonNull::new(e).unwrap())) +pub unsafe fn catch_exception( + closure: impl FnOnce() -> R, +) -> Result>> { + r#try(closure).map_err(|e| NonNull::new(e).map(|e| Id::new(e.cast()))) } diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index 7cfe530d0..a46041286 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -14,10 +14,10 @@ macro_rules! objc_try { ($b:block) => { $crate::exception::catch_exception(|| $b).map_err(|exception| { use alloc::borrow::ToOwned; - if exception.is_null() { - MessageError("Uncaught exception nil".to_owned()) + if let Some(exception) = exception { + MessageError(alloc::format!("Uncaught exception {:?}", exception)) } else { - MessageError(alloc::format!("Uncaught exception {:?}", &**exception)) + MessageError("Uncaught exception nil".to_owned()) } }) };