objc2_foundation/
error.rs1use core::fmt;
2use core::panic::{RefUnwindSafe, UnwindSafe};
3use objc2::msg_send;
4use objc2::rc::Retained;
5use objc2::runtime::NSObject;
6
7use crate::{util, NSError};
8
9impl UnwindSafe for NSError {}
10impl RefUnwindSafe for NSError {}
11
12impl NSError {
14 #[cfg(feature = "NSDictionary")]
16 #[cfg(feature = "NSString")]
17 pub fn new(
18 code: objc2::ffi::NSInteger,
19 domain: &crate::NSErrorDomain,
20 ) -> objc2::rc::Retained<Self> {
21 use objc2::AllocAnyThread;
22 unsafe { Self::initWithDomain_code_userInfo(Self::alloc(), domain, code, None) }
26 }
27}
28
29impl NSError {
31 #[cfg(feature = "NSString")]
32 pub fn NSLocalizedDescriptionKey() -> &'static crate::NSErrorUserInfoKey {
33 unsafe { crate::NSLocalizedDescriptionKey }
34 }
35}
36
37#[cfg(feature = "std")]
38impl std::error::Error for NSError {}
39
40impl fmt::Debug for NSError {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 let mut debug = f.debug_struct("NSError");
43 debug.field("code", &self.code());
44
45 #[cfg(feature = "NSString")]
46 debug.field("domain", &self.domain());
47
48 #[cfg(all(feature = "NSDictionary", feature = "NSString"))]
49 debug.field("userInfo", &self.userInfo());
50
51 debug.finish_non_exhaustive()
52 }
53}
54
55impl fmt::Display for NSError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let desc: Retained<NSObject> = if cfg!(feature = "gnustep-1-7") {
58 let desc: Option<Retained<NSObject>> = unsafe { msg_send![self, localizedDescription] };
61 if let Some(desc) = desc {
62 desc
63 } else {
64 let domain: Retained<NSObject> = unsafe { msg_send![self, domain] };
65 unsafe { util::display_string(&domain, f)? };
67 write!(f, " {}", self.code())?;
68
69 return Ok(());
70 }
71 } else {
72 unsafe { msg_send![self, localizedDescription] }
73 };
74
75 unsafe { util::display_string(&desc, f) }
77 }
78}