Skip to main content

rustorb_core/
exception.rs

1use std::fmt;
2
3use rustorb_giop::{CompletionStatus, SystemExceptionBody};
4
5use crate::stream::InStream;
6
7/// The vendor minor code set id of the OMG (`"OM\0\0"`). Minor codes the
8/// CORBA specification defines are `OMG_VMCID | n`.
9pub const OMG_VMCID: u32 = 0x4f4d_0000;
10
11macro_rules! system_exceptions {
12    ($($variant:ident => $name:literal,)*) => {
13        /// The standard system exceptions of CORBA 3.
14        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15        #[allow(non_camel_case_types, clippy::upper_case_acronyms)]
16        pub enum SystemExceptionKind {
17            $(
18                #[doc = concat!("`CORBA::", $name, "`.")]
19                $variant,
20            )*
21        }
22
23        impl SystemExceptionKind {
24            /// The IDL name, such as `TRANSIENT`.
25            pub fn name(self) -> &'static str {
26                match self {
27                    $(SystemExceptionKind::$variant => $name,)*
28                }
29            }
30
31            /// The repository id, such as `IDL:omg.org/CORBA/TRANSIENT:1.0`.
32            pub fn repository_id(self) -> &'static str {
33                match self {
34                    $(SystemExceptionKind::$variant => concat!("IDL:omg.org/CORBA/", $name, ":1.0"),)*
35                }
36            }
37
38            /// Looks a repository id up.
39            pub fn from_repository_id(id: &str) -> Option<SystemExceptionKind> {
40                match id {
41                    $(concat!("IDL:omg.org/CORBA/", $name, ":1.0") => Some(SystemExceptionKind::$variant),)*
42                    _ => None,
43                }
44            }
45        }
46    };
47}
48
49system_exceptions! {
50    UNKNOWN => "UNKNOWN",
51    BAD_PARAM => "BAD_PARAM",
52    NO_MEMORY => "NO_MEMORY",
53    IMP_LIMIT => "IMP_LIMIT",
54    COMM_FAILURE => "COMM_FAILURE",
55    INV_OBJREF => "INV_OBJREF",
56    NO_PERMISSION => "NO_PERMISSION",
57    INTERNAL => "INTERNAL",
58    MARSHAL => "MARSHAL",
59    INITIALIZE => "INITIALIZE",
60    NO_IMPLEMENT => "NO_IMPLEMENT",
61    BAD_TYPECODE => "BAD_TYPECODE",
62    BAD_OPERATION => "BAD_OPERATION",
63    NO_RESOURCES => "NO_RESOURCES",
64    NO_RESPONSE => "NO_RESPONSE",
65    PERSIST_STORE => "PERSIST_STORE",
66    BAD_INV_ORDER => "BAD_INV_ORDER",
67    TRANSIENT => "TRANSIENT",
68    FREE_MEM => "FREE_MEM",
69    INV_IDENT => "INV_IDENT",
70    INV_FLAG => "INV_FLAG",
71    INTF_REPOS => "INTF_REPOS",
72    BAD_CONTEXT => "BAD_CONTEXT",
73    OBJ_ADAPTER => "OBJ_ADAPTER",
74    DATA_CONVERSION => "DATA_CONVERSION",
75    OBJECT_NOT_EXIST => "OBJECT_NOT_EXIST",
76    TRANSACTION_REQUIRED => "TRANSACTION_REQUIRED",
77    TRANSACTION_ROLLEDBACK => "TRANSACTION_ROLLEDBACK",
78    INVALID_TRANSACTION => "INVALID_TRANSACTION",
79    INV_POLICY => "INV_POLICY",
80    CODESET_INCOMPATIBLE => "CODESET_INCOMPATIBLE",
81    REBIND => "REBIND",
82    TIMEOUT => "TIMEOUT",
83    TRANSACTION_UNAVAILABLE => "TRANSACTION_UNAVAILABLE",
84    TRANSACTION_MODE => "TRANSACTION_MODE",
85    BAD_QOS => "BAD_QOS",
86    INVALID_ACTIVITY => "INVALID_ACTIVITY",
87    ACTIVITY_COMPLETED => "ACTIVITY_COMPLETED",
88    ACTIVITY_REQUIRED => "ACTIVITY_REQUIRED",
89    THREAD_CANCELLED => "THREAD_CANCELLED",
90}
91
92impl fmt::Display for SystemExceptionKind {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        f.write_str(self.name())
95    }
96}
97
98/// Minor codes defined by the CORBA specification (already including
99/// [`OMG_VMCID`]), for the exceptions rustorb raises itself.
100pub mod minor {
101    use super::OMG_VMCID;
102
103    /// `BAD_PARAM`: `string_to_object` failed because of a bad scheme name.
104    pub const BAD_SCHEME_NAME: u32 = OMG_VMCID | 7;
105    /// `BAD_PARAM`: `string_to_object` failed because of a bad address.
106    pub const BAD_ADDRESS: u32 = OMG_VMCID | 8;
107    /// `BAD_PARAM`: `string_to_object` failed because of a bad scheme
108    /// specific part.
109    pub const BAD_SCHEME_SPECIFIC_PART: u32 = OMG_VMCID | 9;
110    /// `BAD_PARAM`: `string_to_object` failed for another reason.
111    pub const STRING_TO_OBJECT_OTHER: u32 = OMG_VMCID | 10;
112    /// `BAD_PARAM`: a `wchar` or `wstring` was used, but no `wchar`
113    /// transmission code set is known for the connection.
114    pub const WCHAR_TCS_NOT_KNOWN: u32 = OMG_VMCID | 23;
115    /// `UNKNOWN`: the target raised a user exception the operation does
116    /// not declare.
117    pub const UNLISTED_USER_EXCEPTION: u32 = OMG_VMCID | 1;
118    /// `BAD_INV_ORDER`: the ORB has shut down.
119    pub const ORB_SHUT_DOWN: u32 = OMG_VMCID | 4;
120    /// `TRANSIENT`: no usable profile in the IOR.
121    pub const NO_USABLE_PROFILE: u32 = OMG_VMCID | 2;
122    /// `TRANSIENT`: the request was cancelled.
123    pub const REQUEST_CANCELLED: u32 = OMG_VMCID | 3;
124    /// `BAD_OPERATION`: the target does not know the operation or
125    /// attribute.
126    pub const UNKNOWN_OPERATION: u32 = OMG_VMCID | 2;
127    /// `BAD_INV_ORDER`: the operation would deadlock, such as
128    /// `shutdown(true)` called from a servant.
129    pub const WOULD_DEADLOCK: u32 = OMG_VMCID | 3;
130    /// `OBJECT_NOT_EXIST`: no object has the key of the request (minor 1,
131    /// as omniORB answers for unknown POAs and object ids).
132    pub const NO_SUCH_OBJECT: u32 = OMG_VMCID | 1;
133    /// `TRANSIENT`: the POA manager of the target is discarding requests.
134    pub const POA_DISCARDING: u32 = OMG_VMCID | 1;
135    /// `OBJ_ADAPTER`: the POA manager of the target is inactive.
136    pub const POA_INACTIVE: u32 = OMG_VMCID | 1;
137    /// `OBJ_ADAPTER`: `incarnate` returned a servant that is already active
138    /// in a POA with UNIQUE_ID.
139    pub const SERVANT_ALREADY_ACTIVE: u32 = OMG_VMCID | 2;
140    /// `OBJ_ADAPTER`: a POA with USE_DEFAULT_SERVANT has no default
141    /// servant.
142    pub const NO_DEFAULT_SERVANT: u32 = OMG_VMCID | 3;
143    /// `OBJ_ADAPTER`: a POA with USE_SERVANT_MANAGER has no servant
144    /// manager, or `set_servant_manager` got the wrong kind.
145    pub const NO_SERVANT_MANAGER: u32 = OMG_VMCID | 4;
146    /// `BAD_PARAM`: a list of policy overrides has two policies of one
147    /// type.
148    pub const DUPLICATE_POLICY: u32 = OMG_VMCID | 30;
149    /// `BAD_PARAM`: `register_initial_reference` was given a nil
150    /// reference.
151    pub const NIL_INITIAL_REFERENCE: u32 = OMG_VMCID | 27;
152    /// `BAD_INV_ORDER`: `set_servant_manager` was called a second time.
153    pub const SERVANT_MANAGER_ALREADY_SET: u32 = OMG_VMCID | 6;
154}
155
156/// A CORBA system exception: one of the standard exceptions (or another
157/// ORB's own), with its minor code and completion status.
158#[derive(Debug, Clone, PartialEq, Eq)]
159pub struct SystemException {
160    /// The repository id, such as `IDL:omg.org/CORBA/TRANSIENT:1.0`.
161    pub repository_id: String,
162    /// The minor code.
163    pub minor: u32,
164    /// Whether the operation completed.
165    pub completed: CompletionStatus,
166}
167
168impl SystemException {
169    /// Creates a standard system exception.
170    pub fn new(kind: SystemExceptionKind, minor: u32, completed: CompletionStatus) -> Self {
171        SystemException {
172            repository_id: kind.repository_id().to_owned(),
173            minor,
174            completed,
175        }
176    }
177
178    /// The standard exception this is, if it is one.
179    pub fn kind(&self) -> Option<SystemExceptionKind> {
180        SystemExceptionKind::from_repository_id(&self.repository_id)
181    }
182
183    /// Whether this is the standard exception `kind`.
184    pub fn is(&self, kind: SystemExceptionKind) -> bool {
185        self.kind() == Some(kind)
186    }
187}
188
189impl From<SystemExceptionBody> for SystemException {
190    fn from(body: SystemExceptionBody) -> Self {
191        SystemException {
192            repository_id: body.exception_id,
193            minor: body.minor_code_value,
194            completed: body.completion_status,
195        }
196    }
197}
198
199impl From<&SystemException> for SystemExceptionBody {
200    fn from(e: &SystemException) -> Self {
201        SystemExceptionBody {
202            exception_id: e.repository_id.clone(),
203            minor_code_value: e.minor,
204            completion_status: e.completed,
205        }
206    }
207}
208
209/// Errors while marshalling arguments are `MARSHAL`, not completed.
210impl From<rustorb_cdr::Error> for SystemException {
211    fn from(_: rustorb_cdr::Error) -> Self {
212        SystemException::new(SystemExceptionKind::MARSHAL, 0, CompletionStatus::No)
213    }
214}
215
216impl fmt::Display for SystemException {
217    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218        let name = match self.kind() {
219            Some(kind) => kind.name(),
220            None => &self.repository_id,
221        };
222        let completed = match self.completed {
223            CompletionStatus::Yes => "COMPLETED_YES",
224            CompletionStatus::No => "COMPLETED_NO",
225            CompletionStatus::Maybe => "COMPLETED_MAYBE",
226        };
227        write!(f, "CORBA::{name} (minor {:#010x}, {completed})", self.minor)
228    }
229}
230
231impl std::error::Error for SystemException {}
232
233/// A user exception raised by the target: its repository id, and the
234/// members, which the caller decodes with [`input`](UserException::input).
235#[derive(Debug, Clone)]
236pub struct UserException {
237    pub(crate) repository_id: String,
238    pub(crate) reply: crate::Reply,
239}
240
241impl UserException {
242    /// The repository id of the exception.
243    pub fn repository_id(&self) -> &str {
244        &self.repository_id
245    }
246
247    /// A stream positioned at the first member, after the repository id.
248    pub fn input(&self) -> Result<InStream<'_>, SystemException> {
249        let mut input = self.reply.input();
250        input.read_string()?;
251        Ok(input)
252    }
253}
254
255impl fmt::Display for UserException {
256    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257        write!(f, "user exception {}", self.repository_id)
258    }
259}
260
261/// The outcome of an invocation that did not return normally.
262#[derive(Debug, Clone)]
263pub enum Exception {
264    /// A system exception, raised by the target or by rustorb.
265    System(SystemException),
266    /// A user exception raised by the target.
267    User(UserException),
268}
269
270impl Exception {
271    /// The system exception, if this is one.
272    pub fn as_system(&self) -> Option<&SystemException> {
273        match self {
274            Exception::System(e) => Some(e),
275            Exception::User(_) => None,
276        }
277    }
278
279    /// The user exception, if this is one.
280    pub fn as_user(&self) -> Option<&UserException> {
281        match self {
282            Exception::User(e) => Some(e),
283            Exception::System(_) => None,
284        }
285    }
286
287    /// The repository id of the exception.
288    pub fn repository_id(&self) -> &str {
289        match self {
290            Exception::System(e) => &e.repository_id,
291            Exception::User(e) => &e.repository_id,
292        }
293    }
294}
295
296impl From<SystemException> for Exception {
297    fn from(e: SystemException) -> Self {
298        Exception::System(e)
299    }
300}
301
302impl fmt::Display for Exception {
303    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304        match self {
305            Exception::System(e) => e.fmt(f),
306            Exception::User(e) => e.fmt(f),
307        }
308    }
309}
310
311impl std::error::Error for Exception {}