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
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
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
71namespace detail
72{
73 template <typename Key>
74 [[noreturn]] inline void ElementNotFoundExceptionHelper(
75 const Key& key,
76 typename boost::enable_if<is_wstring<Key>>::type* = nullptr)
77 {
78 try
79 {
80 BOND_THROW(CoreException,
81 "Map element not found: key: " <<
82 boost::locale::conv::utf_to_utf<char>(
83 string_data(key),
84 string_data(key) + string_length(key),
85 boost::locale::conv::stop));
86 }
87 catch (boost::locale::conv::conversion_error &)
88 {
89 BOND_THROW(CoreException, "Map element not found: key: <bad wstring>");
90 }
91 }
92
93 template <typename Key>
94 [[noreturn]] inline void ElementNotFoundExceptionHelper(
95 const Key& key,
96 typename boost::disable_if<is_wstring<Key>>::type* = nullptr)
97 {
98 BOND_THROW(CoreException,
99 "Map element not found: key: " << key);
100 }
101}
102
103
104template <typename Key>
105[[noreturn]] inline void ElementNotFoundException(const Key& key)
106{
107 detail::ElementNotFoundExceptionHelper(key);
108}
109
110
111[[noreturn]] inline void UnknownProtocolException()
112{
113 BOND_THROW(CoreException,
114 "Unmarshaling failed: unsupported protocol");
115}
116
117
118[[noreturn]] inline void UnknownProtocolException(uint16_t magic)
119{
120 BOND_THROW(CoreException,
121 "Unsupported protocol: "
122 << (char)(magic & 0xFF) << (char)(magic >> 8));
123}
124
125
126[[noreturn]] inline void NothingException()
127{
128 BOND_THROW(CoreException,
129 "Field value is 'nothing'");
130}
131
132
133[[noreturn]] inline void InvalidEnumValueException(const char* value, const char* enum_)
134{
135 BOND_THROW(bond::CoreException,
136 "Unexpected value " << value << " for enum " << enum_);
137}
138
139
140[[noreturn]] inline void InvalidEnumValueException(int32_t value, const char* enum_)
141{
142 BOND_THROW(bond::CoreException,
143 "Unexpected value " << value << " for enum " << enum_);
144}
145
146
147[[noreturn]] inline void RapidJsonException(const char* error, size_t offset)
148{
149 BOND_THROW(CoreException,
150 "JSON parser error: " << error << " at offset " << offset);
151}
152
153
154[[noreturn]] inline void UnicodeConversionException()
155{
156 BOND_THROW(CoreException,
157 "Unicode conversion exception");
158}
159
160
161struct StreamException
162 : Exception
163{
164 StreamException(const char* message)
165 : Exception(message)
166 {}
167};
168
169
170struct SchemaValidateException
171 : CoreException
172{
173 SchemaValidateException(const char* message)
174 : CoreException(message)
175 {}
176};
177
178
179[[noreturn]]
180inline void StructBaseDifferentException(const StructDef& src,
181 const StructDef& dst)
182{
183 BOND_THROW(SchemaValidateException,
184 "Schemas are incompatible; struct base different: "
185 << src.metadata.name << ", " << dst.metadata.name);
186}
187
188
189[[noreturn]]
190inline void RequiredFieldMissingException(const StructDef& s_dst,
191 const FieldDef& f_dst)
192{
193 BOND_THROW(SchemaValidateException,
194 "Schemas are incompatible; required field missing: "
195 << s_dst.metadata.name << "::" << f_dst.metadata.name);
196}
197
198
199[[noreturn]]
200inline void OptionalToRequiredException(const StructDef& s_src,
201 const StructDef& s_dst,
202 const FieldDef& f_src,
203 const FieldDef& f_dst)
204{
205 BOND_THROW(SchemaValidateException,
206 "Schemas are incompatible; required modifier removed: "
207 << s_src.metadata.name << "::" << f_src.metadata.name << ", "
208 << s_dst.metadata.name << "::" << f_dst.metadata.name);
209}
210
211
212[[noreturn]]
213inline void FieldTypeIncompatibleException(const StructDef& s_src,
214 const StructDef& s_dst,
215 const FieldDef& f_src,
216 const FieldDef& f_dst)
217{
218 BOND_THROW(SchemaValidateException,
219 "Schemas are incompatible; field types incompatible: "
220 << s_src.metadata.name << "::" << f_src.metadata.name << ", "
221 << s_dst.metadata.name << "::" << f_dst.metadata.name);
222}
223
224
225[[noreturn]] inline void UnknownSchemaDefException(uint16_t id)
226{
227 BOND_THROW(SchemaValidateException,
228 "Failed to validate schema compatibility; "
229 "SchemaDef contains unknown field: " << id);
230}
231
232} // 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