1use std::fmt;
2
3use crate::Version;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 Cdr(rustorb_cdr::Error),
14 BadMagic([u8; 4]),
16 UnsupportedVersion(Version),
18 UnknownMessageType(u8),
20 InvalidFlags(u8),
22 Truncated {
24 expected: usize,
26 actual: usize,
28 },
29 TrailingBytes(usize),
31 MessageTooLarge(usize),
33 InvalidAddressingDisposition(i16),
35 TargetNotKey,
37 InvalidReplyStatus(u32),
39 InvalidLocateStatus(u32),
41 UnexpectedFragment,
43 FragmentInProgress,
46 FragmentMismatch,
48 FragmentTooLarge(usize),
50 FragmentNotSupported,
52 InvalidIorString(&'static str),
54 InvalidObjectUrl(&'static str),
57 UnexpectedProfileTag(u32),
59 UnsupportedIiopVersion(Version),
61 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}