Skip to main content

serde_xdr/ser/
errors.rs

1use {
2    failure::{Compat, Fail},
3    serde::ser,
4    std::{
5        error,
6        fmt::{self, Display, Formatter},
7        io, result,
8    },
9};
10
11/// Error during serialization.
12#[derive(Debug, Fail)]
13pub enum SerializationError {
14    /// Custom error message.
15    #[fail(display = "custom error message: {}", message)]
16    Custom {
17        /// The message of the custom error.
18        message: String,
19    },
20
21    /// Failure to serialize a value.
22    #[fail(display = "failed to serialize {}", what)]
23    Failure {
24        /// A description of what was being serialized.
25        what: String,
26        /// The error that ocurred during serialization.
27        #[cause]
28        cause: Box<CompatSerializationError>,
29    },
30
31    /// IO error while serializing a value.
32    #[fail(display = "IO error while serializing {}: {}", what, cause)]
33    IoError {
34        /// A description of what was being serialized.
35        what: String,
36        /// The error that ocurred during serialization.
37        #[cause]
38        cause: io::Error,
39    },
40
41    /// Map types are not supported by XDR.
42    #[fail(display = "XDR does not support a map type")]
43    MapIsNotSupported,
44
45    /// Attempt to serialize opaque data with too many bytes.
46    #[fail(display = "opaque data is too long: {} bytes", length)]
47    OpaqueDataIsTooLong {
48        /// The length of the data, which is larger than what can be
49        /// represented.
50        length: usize,
51    },
52
53    /// Fatal error while serializing a sequence or a tuple.
54    ///
55    /// This is probably caused by ignoring a previous error.
56    #[fail(display = "fatal failure while serializing {}", type_name)]
57    SequenceOrTupleFatalError {
58        /// The name of the type being serialized.
59        type_name: String,
60    },
61
62    /// Fatal error while serializing an object.
63    ///
64    /// This is probably caused by ignoring a previous error.
65    #[fail(display = "fatal failure while serializing struct: {}", name)]
66    StructFatalError {
67        /// The name of the type being serialized.
68        name: String,
69    },
70
71    /// Attempt to serialize a sequence that's too long.
72    #[fail(display = "sequence is too long to be serialized: {}", length)]
73    SequenceTooLong {
74        /// The length of the sequence, which is larger than what can be
75        /// represented.
76        length: usize,
77    },
78
79    /// Sequences with unknown lengths are not supported.
80    #[fail(display = "can't serialize sequence with unknown length")]
81    SequenceWithUnknownLength,
82
83    /// Only ASCII strings can be serialized.
84    #[fail(display = "string is not ASCII encoded: {}", string)]
85    StringIsNotAscii {
86        /// The string that can't be represented as an ASCII string.
87        string: String,
88    },
89
90    /// Attempt to serialize a string that's too long.
91    #[fail(display = "string is too long: {}", string)]
92    StringIsTooLong {
93        /// The length of the string, which is larger than what can be
94        /// represented.
95        string: String,
96    },
97}
98
99/// An `Error`-compatible wrapper for `SerializationError`.
100///
101/// Contains helper methods to convert to and from the wrapped type.
102#[derive(Debug)]
103pub struct CompatSerializationError(Compat<SerializationError>);
104
105impl From<SerializationError> for CompatSerializationError {
106    fn from(error: SerializationError) -> Self {
107        CompatSerializationError(error.compat())
108    }
109}
110
111impl Display for CompatSerializationError {
112    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
113        self.0.fmt(formatter)
114    }
115}
116
117impl error::Error for CompatSerializationError {
118    fn description(&self) -> &str {
119        self.0.description()
120    }
121}
122
123impl ser::Error for CompatSerializationError {
124    fn custom<T: Display>(message: T) -> Self {
125        let error = SerializationError::Custom {
126            message: message.to_string(),
127        };
128
129        CompatSerializationError(error.compat())
130    }
131}
132
133impl From<CompatSerializationError> for SerializationError {
134    fn from(wrapped_error: CompatSerializationError) -> Self {
135        match wrapped_error {
136            CompatSerializationError(error) => error.into_inner(),
137        }
138    }
139}
140
141pub type Result<T> = result::Result<T, CompatSerializationError>;