Bond
 
Loading...
Searching...
No Matches
exception.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include "detail/string_stream.h"
9
10#include <bond/core/bond_types.h>
11
12#include <boost/locale/encoding_utf.hpp>
13#include <boost/utility/enable_if.hpp>
14
15#define BOND_THROW(x, y) throw x((::bond::detail::basic_string_stream<1024>() << y).content());
16
17namespace bond
18{
19
21class Exception
22 : public std::exception,
23 public SerializableExceptionBase
24{
25public:
26 const char* what() const BOND_NOEXCEPT
27 {
28 return message.c_str();
29 }
30
31 virtual ~Exception() BOND_NOEXCEPT
32 {}
33
34protected:
35 Exception(const char* msg) BOND_NOEXCEPT
36 {
37 message = msg;
38 }
39
40 Exception() BOND_NOEXCEPT
41 {}
42};
43
44
47struct CoreException
48 : Exception
49{
50 CoreException(const char* message)
51 : Exception(message)
52 {}
53};
54
55
56[[noreturn]] inline void MergerContainerException(uint32_t payload, uint32_t obj)
57{
58 BOND_THROW(CoreException,
59 "Merge failed: container mismatch, length in the payload: "
60 << payload << " length in the object: " << obj);
61}
62
63
64[[noreturn]] inline void InvalidKeyTypeException()
65{
66 BOND_THROW(CoreException,
67 "Map key type not valid");
68}
69
70[[noreturn]] inline void UnknownDataTypeException()
71{
72 BOND_THROW(CoreException,
73 "Unknown data type found");
74}
75
76[[noreturn]] inline void ExceededMaxRecursionDepthException()
77{
78 BOND_THROW(CoreException,
79 "Max recursion depth exceeded");
80}
81
82[[noreturn]] inline void OutOfBoundObjectSizeException()
83{
84 BOND_THROW(CoreException,
85 "Payload had an element size larger than the input buffer");
86}
87
88[[noreturn]] inline void OutOfBoundStringSizeException()
89{
90 BOND_THROW(CoreException,
91 "Payload-specified string length exceeds the input buffer size");
92}
93
94namespace detail
95{
96 template <typename Key>
97 [[noreturn]] inline void ElementNotFoundExceptionHelper(
98 const Key& key,
99 typename boost::enable_if<is_wstring<Key>>::type* = nullptr)
100 {
101 try
102 {
103 BOND_THROW(CoreException,
104 "Map element not found: key: " <<
105 boost::locale::conv::utf_to_utf<char>(
106 string_data(key),
107 string_data(key) + string_length(key),
108 boost::locale::conv::stop));
109 }
110 catch (boost::locale::conv::conversion_error &)
111 {
112 BOND_THROW(CoreException, "Map element not found: key: <bad wstring>");
113 }
114 }
115
116 template <typename Key>
117 [[noreturn]] inline void ElementNotFoundExceptionHelper(
118 const Key& key,
119 typename boost::disable_if<is_wstring<Key>>::type* = nullptr)
120 {
121 BOND_THROW(CoreException,
122 "Map element not found: key: " << key);
123 }
124}
125
126
127template <typename Key>
128[[noreturn]] inline void ElementNotFoundException(const Key& key)
129{
130 detail::ElementNotFoundExceptionHelper(key);
131}
132
133
134[[noreturn]] inline void UnknownProtocolException()
135{
136 BOND_THROW(CoreException,
137 "Unmarshaling failed: unsupported protocol");
138}
139
140
141[[noreturn]] inline void UnknownProtocolException(uint16_t magic)
142{
143 BOND_THROW(CoreException,
144 "Unsupported protocol: "
145 << (char)(magic & 0xFF) << (char)(magic >> 8));
146}
147
148
149[[noreturn]] inline void NothingException()
150{
151 BOND_THROW(CoreException,
152 "Field value is 'nothing'");
153}
154
155
156[[noreturn]] inline void InvalidEnumValueException(const char* value, const char* enum_)
157{
158 BOND_THROW(bond::CoreException,
159 "Unexpected value " << value << " for enum " << enum_);
160}
161
162
163[[noreturn]] inline void InvalidEnumValueException(int32_t value, const char* enum_)
164{
165 BOND_THROW(bond::CoreException,
166 "Unexpected value " << value << " for enum " << enum_);
167}
168
169
170[[noreturn]] inline void RapidJsonException(const char* error, size_t offset)
171{
172 BOND_THROW(CoreException,
173 "JSON parser error: " << error << " at offset " << offset);
174}
175
176
177[[noreturn]] inline void UnicodeConversionException()
178{
179 BOND_THROW(CoreException,
180 "Unicode conversion exception");
181}
182
183
184struct StreamException
185 : Exception
186{
187 StreamException(const char* message)
188 : Exception(message)
189 {}
190};
191
192
193struct SchemaValidateException
195{
196 SchemaValidateException(const char* message)
197 : CoreException(message)
198 {}
199};
200
201
202[[noreturn]]
203inline void StructBaseDifferentException(const StructDef& src,
204 const StructDef& dst)
205{
206 BOND_THROW(SchemaValidateException,
207 "Schemas are incompatible; struct base different: "
208 << src.metadata.name << ", " << dst.metadata.name);
209}
210
211
212[[noreturn]]
213inline void RequiredFieldMissingException(const StructDef& s_dst,
214 const FieldDef& f_dst)
215{
216 BOND_THROW(SchemaValidateException,
217 "Schemas are incompatible; required field missing: "
218 << s_dst.metadata.name << "::" << f_dst.metadata.name);
219}
220
221
222[[noreturn]]
223inline void OptionalToRequiredException(const StructDef& s_src,
224 const StructDef& s_dst,
225 const FieldDef& f_src,
226 const FieldDef& f_dst)
227{
228 BOND_THROW(SchemaValidateException,
229 "Schemas are incompatible; required modifier removed: "
230 << s_src.metadata.name << "::" << f_src.metadata.name << ", "
231 << s_dst.metadata.name << "::" << f_dst.metadata.name);
232}
233
234
235[[noreturn]]
236inline void FieldTypeIncompatibleException(const StructDef& s_src,
237 const StructDef& s_dst,
238 const FieldDef& f_src,
239 const FieldDef& f_dst)
240{
241 BOND_THROW(SchemaValidateException,
242 "Schemas are incompatible; field types incompatible: "
243 << s_src.metadata.name << "::" << f_src.metadata.name << ", "
244 << s_dst.metadata.name << "::" << f_dst.metadata.name);
245}
246
247
248[[noreturn]] inline void UnknownSchemaDefException(uint16_t id)
249{
250 BOND_THROW(SchemaValidateException,
251 "Failed to validate schema compatibility; "
252 "SchemaDef contains unknown field: " << id);
253}
254
255} // namespace bond
Base type for all Bond exceptions.
Definition exception.h:24
namespace bond
Definition apply.h:17
Exception used to indicate an error during serialization or deserialization.
Definition exception.h:49