objc2_foundation/
error.rs

1use 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
12/// Creation methods.
13impl NSError {
14    /// Construct a new [`NSError`] with the given code in the given domain.
15    #[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        // SAFETY: `domain` and `user_info` are copied to the error object, so
23        // even if the `&NSString` came from a `&mut NSMutableString`, we're
24        // still good!
25        unsafe { Self::initWithDomain_code_userInfo(Self::alloc(), domain, code, None) }
26    }
27}
28
29/// Accessor methods.
30impl 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            // Can return NULL:
59            // https://github.com/gnustep/libs-base/issues/486
60            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                // SAFETY: `domain` returns `NSErrorDomain`, which is `NSString`.
66                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        // SAFETY: `localizedDescription` returns `NSString`.
76        unsafe { util::display_string(&desc, f) }
77    }
78}