Skip to main content

rustorb_naming/
error.rs

1use std::fmt;
2
3use rustorb_core::name::{self, InvalidName, Name};
4use rustorb_core::{CompletionStatus, SystemException, SystemExceptionKind, minor};
5
6use crate::client::{NamingContext, from_idl_name, to_idl_name};
7use crate::idl::cos_naming::{self as idl, naming_context as nc, naming_context_ext as nce};
8use crate::repository_id;
9
10/// `CosNaming::NamingContext::NotFoundReason`: why a name was not found.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum NotFoundReason {
13    /// `missing_node`: the first component of the rest of the name is not
14    /// bound.
15    MissingNode,
16    /// `not_context`: the first component of the rest of the name is bound
17    /// to an object, where a context was needed.
18    NotContext,
19    /// `not_object`: the first component of the rest of the name is bound
20    /// to a context, where an object was needed.
21    NotObject,
22}
23
24impl NotFoundReason {
25    /// The IDL name, such as `missing_node`.
26    pub fn name(self) -> &'static str {
27        match self {
28            NotFoundReason::MissingNode => "missing_node",
29            NotFoundReason::NotContext => "not_context",
30            NotFoundReason::NotObject => "not_object",
31        }
32    }
33}
34
35impl fmt::Display for NotFoundReason {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.write_str(self.name())
38    }
39}
40
41/// How a naming operation failed: one of the CosNaming user exceptions, or
42/// a system exception.
43///
44/// Which user exceptions an operation can raise is listed on it. A user
45/// exception the operation does not declare becomes `UNKNOWN` (minor
46/// `UNLISTED_USER_EXCEPTION`), as for any IDL stub.
47#[derive(Debug, Clone)]
48pub enum NamingError {
49    /// `NamingContext::NotFound`: resolving `rest_of_name` failed for
50    /// `why`.
51    NotFound {
52        /// Why the name was not found.
53        why: NotFoundReason,
54        /// The part of the name not resolved, from the component that
55        /// failed.
56        rest_of_name: Name,
57    },
58    /// `NamingContext::CannotProceed`: the naming service gave up on
59    /// `rest_of_name`, which the client may resolve in `context` itself.
60    CannotProceed {
61        /// The context to continue in.
62        context: Option<NamingContext>,
63        /// The part of the name not resolved.
64        rest_of_name: Name,
65    },
66    /// `NamingContext::InvalidName`: the name is empty or a component is
67    /// not valid. Also raised locally for a malformed stringified name.
68    InvalidName,
69    /// `NamingContext::AlreadyBound`: `bind` on a name that is bound.
70    AlreadyBound,
71    /// `NamingContext::NotEmpty`: `destroy` on a context with bindings.
72    NotEmpty,
73    /// `NamingContextExt::InvalidAddress`: `to_url` with an address that is
74    /// not a valid `corbaloc` address list.
75    InvalidAddress,
76    /// A system exception.
77    System(SystemException),
78}
79
80impl NamingError {
81    /// The repository id of the exception.
82    pub fn repository_id(&self) -> &str {
83        match self {
84            NamingError::NotFound { .. } => repository_id::NOT_FOUND,
85            NamingError::CannotProceed { .. } => repository_id::CANNOT_PROCEED,
86            NamingError::InvalidName => repository_id::INVALID_NAME,
87            NamingError::AlreadyBound => repository_id::ALREADY_BOUND,
88            NamingError::NotEmpty => repository_id::NOT_EMPTY,
89            NamingError::InvalidAddress => repository_id::INVALID_ADDRESS,
90            NamingError::System(e) => &e.repository_id,
91        }
92    }
93
94    /// The system exception, if this is one.
95    pub fn as_system(&self) -> Option<&SystemException> {
96        match self {
97            NamingError::System(e) => Some(e),
98            _ => None,
99        }
100    }
101}
102
103// --- the exceptions of the generated code (crate::idl) ----------------------
104
105impl From<nc::NotFoundReason> for NotFoundReason {
106    fn from(why: nc::NotFoundReason) -> NotFoundReason {
107        match why {
108            nc::NotFoundReason::MissingNode => NotFoundReason::MissingNode,
109            nc::NotFoundReason::NotContext => NotFoundReason::NotContext,
110            nc::NotFoundReason::NotObject => NotFoundReason::NotObject,
111        }
112    }
113}
114
115impl From<NotFoundReason> for nc::NotFoundReason {
116    fn from(why: NotFoundReason) -> nc::NotFoundReason {
117        match why {
118            NotFoundReason::MissingNode => nc::NotFoundReason::MissingNode,
119            NotFoundReason::NotContext => nc::NotFoundReason::NotContext,
120            NotFoundReason::NotObject => nc::NotFoundReason::NotObject,
121        }
122    }
123}
124
125impl From<nc::NotFound> for NamingError {
126    fn from(e: nc::NotFound) -> NamingError {
127        NamingError::NotFound {
128            why: e.why.into(),
129            rest_of_name: from_idl_name(e.rest_of_name),
130        }
131    }
132}
133
134impl From<nc::CannotProceed> for NamingError {
135    fn from(e: nc::CannotProceed) -> NamingError {
136        NamingError::CannotProceed {
137            context: e
138                .cxt
139                .map(|c| NamingContext::unchecked_narrow(c.into_object())),
140            rest_of_name: from_idl_name(e.rest_of_name),
141        }
142    }
143}
144
145impl From<nc::InvalidName> for NamingError {
146    fn from(_: nc::InvalidName) -> NamingError {
147        NamingError::InvalidName
148    }
149}
150
151impl From<nc::AlreadyBound> for NamingError {
152    fn from(_: nc::AlreadyBound) -> NamingError {
153        NamingError::AlreadyBound
154    }
155}
156
157impl From<nc::NotEmpty> for NamingError {
158    fn from(_: nc::NotEmpty) -> NamingError {
159        NamingError::NotEmpty
160    }
161}
162
163impl From<nce::InvalidAddress> for NamingError {
164    fn from(_: nce::InvalidAddress) -> NamingError {
165        NamingError::InvalidAddress
166    }
167}
168
169/// A [`NamingError`] as the user exception of the generated code, for the
170/// servants.
171enum User {
172    NotFound(nc::NotFound),
173    CannotProceed(nc::CannotProceed),
174    InvalidName(nc::InvalidName),
175    AlreadyBound(nc::AlreadyBound),
176    NotEmpty(nc::NotEmpty),
177    InvalidAddress(nce::InvalidAddress),
178}
179
180impl NamingError {
181    fn into_user(self) -> Result<User, SystemException> {
182        Ok(match self {
183            NamingError::NotFound { why, rest_of_name } => User::NotFound(nc::NotFound {
184                why: why.into(),
185                rest_of_name: to_idl_name(&rest_of_name),
186            }),
187            NamingError::CannotProceed {
188                context,
189                rest_of_name,
190            } => User::CannotProceed(nc::CannotProceed {
191                cxt: context.map(|c| idl::NamingContext::unchecked_narrow(c.into_object())),
192                rest_of_name: to_idl_name(&rest_of_name),
193            }),
194            NamingError::InvalidName => User::InvalidName(nc::InvalidName {}),
195            NamingError::AlreadyBound => User::AlreadyBound(nc::AlreadyBound {}),
196            NamingError::NotEmpty => User::NotEmpty(nc::NotEmpty {}),
197            NamingError::InvalidAddress => User::InvalidAddress(nce::InvalidAddress {}),
198            NamingError::System(e) => return Err(e),
199        })
200    }
201}
202
203/// Converts between [`NamingError`] and the error enum of an operation of
204/// the generated code, whose user exceptions are `$user`. A user exception
205/// the operation does not declare becomes `UNKNOWN`, as a skeleton sends it.
206macro_rules! errors {
207    ($($ty:ty { $($user:ident),* })*) => {
208        $(
209            impl From<$ty> for NamingError {
210                fn from(e: $ty) -> NamingError {
211                    type E = $ty;
212                    match e {
213                        $(E::$user(e) => e.into(),)*
214                        E::System(e) => NamingError::System(e),
215                    }
216                }
217            }
218
219            impl From<NamingError> for $ty {
220                fn from(e: NamingError) -> $ty {
221                    type E = $ty;
222                    match e.into_user() {
223                        $(Ok(User::$user(e)) => E::$user(e),)*
224                        #[allow(unreachable_patterns)]
225                        Ok(_) => E::System(SystemException::new(
226                            SystemExceptionKind::UNKNOWN,
227                            minor::UNLISTED_USER_EXCEPTION,
228                            CompletionStatus::Maybe,
229                        )),
230                        Err(e) => E::System(e),
231                    }
232                }
233            }
234        )*
235    };
236}
237
238errors! {
239    nc::BindError { NotFound, CannotProceed, InvalidName, AlreadyBound }
240    nc::RebindError { NotFound, CannotProceed, InvalidName }
241    nc::BindContextError { NotFound, CannotProceed, InvalidName, AlreadyBound }
242    nc::RebindContextError { NotFound, CannotProceed, InvalidName }
243    nc::ResolveError { NotFound, CannotProceed, InvalidName }
244    nc::UnbindError { NotFound, CannotProceed, InvalidName }
245    nc::BindNewContextError { NotFound, CannotProceed, InvalidName, AlreadyBound }
246    nc::DestroyError { NotEmpty }
247    nce::ToStringError { InvalidName }
248    nce::ToNameError { InvalidName }
249    nce::ToUrlError { InvalidAddress, InvalidName }
250    nce::ResolveStrError { NotFound, CannotProceed, InvalidName }
251}
252
253impl From<SystemException> for NamingError {
254    fn from(e: SystemException) -> Self {
255        NamingError::System(e)
256    }
257}
258
259impl From<InvalidName> for NamingError {
260    fn from(_: InvalidName) -> Self {
261        NamingError::InvalidName
262    }
263}
264
265impl fmt::Display for NamingError {
266    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267        let rest = |name: &Name| name::to_string(name).unwrap_or_else(|_| "<empty>".to_owned());
268        match self {
269            NamingError::NotFound { why, rest_of_name } => {
270                write!(
271                    f,
272                    "CosNaming::NamingContext::NotFound ({why}, rest {:?})",
273                    rest(rest_of_name)
274                )
275            }
276            NamingError::CannotProceed { rest_of_name, .. } => {
277                write!(
278                    f,
279                    "CosNaming::NamingContext::CannotProceed (rest {:?})",
280                    rest(rest_of_name)
281                )
282            }
283            NamingError::InvalidName => f.write_str("CosNaming::NamingContext::InvalidName"),
284            NamingError::AlreadyBound => f.write_str("CosNaming::NamingContext::AlreadyBound"),
285            NamingError::NotEmpty => f.write_str("CosNaming::NamingContext::NotEmpty"),
286            NamingError::InvalidAddress => {
287                f.write_str("CosNaming::NamingContextExt::InvalidAddress")
288            }
289            NamingError::System(e) => e.fmt(f),
290        }
291    }
292}
293
294impl std::error::Error for NamingError {}