Skip to main content

rustorb_giop/
error.rs

1use std::fmt;
2
3use crate::Version;
4
5/// Result type of GIOP operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors raised while reading or writing GIOP messages.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// A CDR error in a header or body.
13    Cdr(rustorb_cdr::Error),
14    /// The message did not start with `GIOP`.
15    BadMagic([u8; 4]),
16    /// The GIOP version is not 1.0, 1.1 or 1.2.
17    UnsupportedVersion(Version),
18    /// The message type is unknown or not valid for the version.
19    UnknownMessageType(u8),
20    /// The GIOP 1.0 byte order octet was neither `0` nor `1`.
21    InvalidFlags(u8),
22    /// Fewer bytes than the header and message size require.
23    Truncated {
24        /// Bytes the message needs.
25        expected: usize,
26        /// Bytes available.
27        actual: usize,
28    },
29    /// Bytes left over after the message.
30    TrailingBytes(usize),
31    /// The message does not fit in the 32-bit message size.
32    MessageTooLarge(usize),
33    /// A `TargetAddress` had an unknown discriminator.
34    InvalidAddressingDisposition(i16),
35    /// GIOP 1.0 and 1.1 can only address an object by key.
36    TargetNotKey,
37    /// A `ReplyStatusType` value was unknown.
38    InvalidReplyStatus(u32),
39    /// A `LocateStatusType` value was unknown.
40    InvalidLocateStatus(u32),
41    /// A fragment arrived with no message to continue.
42    UnexpectedFragment,
43    /// A GIOP 1.1 message started fragmenting while another was in progress,
44    /// or a GIOP 1.2 request id was reused while in progress.
45    FragmentInProgress,
46    /// A fragment's version or byte order differs from the first message.
47    FragmentMismatch,
48    /// The reassembled message would exceed the configured limit.
49    FragmentTooLarge(usize),
50    /// GIOP 1.0 has no fragmentation.
51    FragmentNotSupported,
52    /// A stringified IOR was malformed.
53    InvalidIorString(&'static str),
54    /// A `corbaloc:` or `corbaname:` URL was malformed, or had an unknown
55    /// scheme.
56    InvalidObjectUrl(&'static str),
57    /// A profile had a different tag than the one being read.
58    UnexpectedProfileTag(u32),
59    /// An IIOP profile's major version was not 1.
60    UnsupportedIiopVersion(Version),
61    /// An IIOP 1.0 profile cannot carry tagged components.
62    ComponentsInIiop1_0,
63}
64
65impl fmt::Display for Error {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            Error::Cdr(e) => write!(f, "CDR error: {e}"),
69            Error::BadMagic(m) => write!(f, "bad GIOP magic {m:02x?}"),
70            Error::UnsupportedVersion(v) => write!(f, "unsupported GIOP version {v}"),
71            Error::UnknownMessageType(t) => write!(f, "unknown GIOP message type {t}"),
72            Error::InvalidFlags(b) => write!(f, "invalid GIOP 1.0 byte order {b:#04x}"),
73            Error::Truncated { expected, actual } => {
74                write!(
75                    f,
76                    "GIOP message truncated: need {expected} bytes, got {actual}"
77                )
78            }
79            Error::TrailingBytes(n) => write!(f, "{n} bytes after the GIOP message"),
80            Error::MessageTooLarge(n) => write!(f, "GIOP message of {n} bytes is too large"),
81            Error::InvalidAddressingDisposition(d) => {
82                write!(f, "invalid addressing disposition {d}")
83            }
84            Error::TargetNotKey => f.write_str("GIOP 1.0/1.1 requires an object key target"),
85            Error::InvalidReplyStatus(v) => write!(f, "invalid reply status {v}"),
86            Error::InvalidLocateStatus(v) => write!(f, "invalid locate status {v}"),
87            Error::UnexpectedFragment => f.write_str("fragment without a message to continue"),
88            Error::FragmentInProgress => f.write_str("a fragmented message is already in progress"),
89            Error::FragmentMismatch => {
90                f.write_str("fragment version or byte order differs from the first message")
91            }
92            Error::FragmentTooLarge(n) => {
93                write!(f, "reassembled message of {n} bytes exceeds the limit")
94            }
95            Error::FragmentNotSupported => f.write_str("GIOP 1.0 does not support fragments"),
96            Error::InvalidIorString(why) => write!(f, "invalid stringified IOR: {why}"),
97            Error::InvalidObjectUrl(why) => write!(f, "invalid object URL: {why}"),
98            Error::UnexpectedProfileTag(tag) => write!(f, "unexpected profile tag {tag}"),
99            Error::UnsupportedIiopVersion(v) => write!(f, "unsupported IIOP version {v}"),
100            Error::ComponentsInIiop1_0 => f.write_str("IIOP 1.0 profiles cannot have components"),
101        }
102    }
103}
104
105impl std::error::Error for Error {
106    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
107        match self {
108            Error::Cdr(e) => Some(e),
109            _ => None,
110        }
111    }
112}
113
114impl From<rustorb_cdr::Error> for Error {
115    fn from(e: rustorb_cdr::Error) -> Error {
116        Error::Cdr(e)
117    }
118}