20
20
#pragma once
21
21
22
22
#include < exception>
23
+ #include < sstream>
23
24
#include < string>
24
25
25
26
namespace eCAL
26
27
{
27
- /* @cond */
28
+ /* *
29
+ * @brief Exception thrown when an error occurs during deserialization.
30
+ *
31
+ * This exception is raised when any kind of error occurs during the deserialization process,
32
+ * such as when the data is corrupt or does not match the expected format.
33
+ */
28
34
class DeserializationException : public std ::exception
29
35
{
30
36
public:
37
+ /* *
38
+ * @brief Constructs a new DeserializationException object.
39
+ *
40
+ * Initializes the exception with a descriptive error message.
41
+ *
42
+ * @param message A string containing a detailed description of the error.
43
+ */
31
44
DeserializationException (const std::string& message) : message_(message) {}
32
45
virtual const char * what () const noexcept { return message_.c_str (); }
33
46
private:
34
47
std::string message_;
35
48
};
36
- /* @endcond */
49
+
50
+ /* *
51
+ * @brief Exception thrown when a given datatype is incompatible with a serializer.
52
+ *
53
+ * This exception is raised when a serializer cannot accept the provided datatype,
54
+ * for example when creating an input channel or subscriber. It encapsulates both the
55
+ * expected and the actual datatype information to facilitate debugging and error handling.
56
+ *
57
+ * @tparam DatatypeInformation A type that holds information about a datatype.
58
+ * It is expected to have at least the members `encoding` and `name`.
59
+ */
60
+ template <typename DatatypeInformation>
61
+ class TypeMismatchException : public std ::exception
62
+ {
63
+ public:
64
+ /* *
65
+ * @brief Constructs a new TypeMismatchException object.
66
+ *
67
+ * Initializes the exception with the expected and actual datatype information.
68
+ * An error message is generated detailing the mismatch between the two datatypes.
69
+ *
70
+ * @param expected_datatype_ The datatype information that was expected.
71
+ * @param actual_datatype_ The datatype information that was actually provided.
72
+ */
73
+ TypeMismatchException (const DatatypeInformation& expected_datatype_,
74
+ const DatatypeInformation& actual_datatype_)
75
+ : expected_datatype(expected_datatype_),
76
+ actual_datatype (actual_datatype_)
77
+ {
78
+ std::ostringstream oss;
79
+ oss << " Type Mismatch error: Expected and actual datatypes do not match\n "
80
+ << " Expected type:\n "
81
+ << " encoding: " << expected_datatype.encoding << " \n "
82
+ << " name: " << expected_datatype.name << " \n "
83
+ << " Actual type:\n "
84
+ << " encoding: " << actual_datatype.encoding << " \n "
85
+ << " name: " << actual_datatype.name << " \n " ;
86
+ error_message = oss.str ();
87
+ }
88
+
89
+ virtual const char * what () const noexcept override {
90
+ return error_message.c_str ();
91
+ }
92
+
93
+ // Optionally, add accessors for the datatype information.
94
+ const DatatypeInformation& getExpectedDatatype () const noexcept {
95
+ return expected_datatype;
96
+ }
97
+
98
+ const DatatypeInformation& getActualDatatype () const noexcept {
99
+ return actual_datatype;
100
+ }
101
+
102
+ private:
103
+ const DatatypeInformation expected_datatype;
104
+ const DatatypeInformation actual_datatype;
105
+ std::string error_message; // Store the error message persistently.
106
+ };
37
107
}
0 commit comments