userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/protobuf/json/exceptions.hpp
4
/// @brief Exceptions thrown by the JSON utilities.
5
6
#
include
<
string
>
7
#
include
<
type_traits
>
8
9
#
include
<
userver
/
protobuf
/
exceptions
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
/// @brief Top namespace for the protobuf JSON utilities.
14
namespace
protobuf::json {
15
16
/// @brief JSON `Value` to protobuf message conversion error code.
17
enum
class
ParseErrorCode
{
18
/// @brief JSON field is unknown (does not match to any protobuf message field).
19
kUnknownField
= 1,
20
21
/// @brief Enum value name used as a JSON field value is unknown.
22
kUnknownEnum
= 2,
23
24
/// @brief JSON contains more than one field for the same oneof in the protobuf message.
25
kMultipleOneofFields
= 3,
26
27
/// @brief JSON field type is not compatible with corresponding protobuf message field type.
28
kInvalidType
= 4,
29
30
/// @brief JSON field value is invalid according to ProtoJSON rules.
31
kInvalidValue
= 5
32
};
33
34
/// @brief Protobuf message to JSON `Value` conversion error code.
35
enum
class
PrintErrorCode
{
36
/// @brief Protobuf message field has invalid value.
37
/// This code can be set when converting well-known message which may have more strict constraints than the
38
/// types of their fields (see
39
/// [here](https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/duration.proto) for example).
40
kInvalidValue
= 1
41
};
42
43
/// @brief JSON/protobuf conversion error information.
44
/// @tparam TErrorCode error code type
45
template
<
typename
TErrorCode>
46
requires
(std::is_same_v<TErrorCode,
ParseErrorCode
> || std::is_same_v<TErrorCode,
PrintErrorCode
>)
47
class
ConversionErrorInfo
{
48
public
:
49
using
ErrorCodeType = TErrorCode;
50
51
/// @brief Creates error information for invalid JSON/protobuf field identified by @a path .
52
/// See @ref ConversionErrorInfo::GetPath for more information about @a path format.
53
ConversionErrorInfo
(
const
ErrorCodeType code, std::string path)
noexcept
: code_(code), path_(std::move(path)) {}
54
55
/// @brief Returns error code.
56
[[
nodiscard
]] ErrorCodeType
GetCode
()
const
noexcept
{
return
code_; }
57
58
/// @brief Returns invalid field path.
59
/// Parameter @a path format depends on the conversion direction:
60
/// 1. In case of JSON to protobuf conversion, @a path will be formatted in a way used for exceptions thrown by
61
/// by various `ValueBuilder` and `Value` class methods (see@ref formats::json::ExceptionWithPath): `/` (root),
62
/// `field.array[0].item`, etc. Field names will be taken from JSON and **may not match** target field names in
63
/// the protobuf message (ProtoJSON by default uses `lowerCamelCase`-encoded protobuf field names as JSON field
64
/// names).
65
/// 2. In case of protobuf message to JSON conversion, @a path will be formatted in a similar way as above, however
66
/// `map` types will be handled explicitly. Examples: `/` (root), `field.repeated[0].item.map['key'].value`, etc.
67
/// Field names will be taken from the protobuf message and **may not match** target field names in the JSON.
68
[[
nodiscard
]]
const
std::string&
GetPath
()
const
&
noexcept
{
return
path_; }
69
70
/// @brief Returns invalid field path.
71
/// See @ref ConversionErrorInfo::GetPath for more information about @a path format.
72
[[
nodiscard
]] std::string
GetPath
() &&
noexcept
{
return
std::move(path_); }
73
74
private
:
75
ErrorCodeType code_;
76
std::string path_;
77
};
78
79
/// @brief JSON `Value` to protobuf message conversion error information.
80
using
ParseErrorInfo
=
ConversionErrorInfo
<
ParseErrorCode
>;
81
82
/// @brief Protobuf message to JSON `Value` conversion error information.
83
using
PrintErrorInfo
=
ConversionErrorInfo
<
PrintErrorCode
>;
84
85
/// @brief Base exception type for JSON utilities.
86
class
JsonError
:
public
protobuf::
Error
{
87
public
:
88
using
protobuf::
Error
::Error;
89
};
90
91
/// @brief Base exception type for JSON/protobuf conversion errors.
92
class
ConversionErrorBase
:
public
protobuf::
Error
{
93
public
:
94
using
protobuf::
Error
::Error;
95
};
96
97
/// @brief JSON/protobuf conversion error.
98
/// @tparam TErrorCode error code type
99
template
<
typename
TErrorCode>
100
class
ConversionError
:
public
ConversionErrorBase
{
101
public
:
102
using
ErrorInfoType =
ConversionErrorInfo
<TErrorCode>;
103
using
ErrorCodeType =
typename
ErrorInfoType::ErrorCodeType;
104
105
/// @brief Creates error for invalid json/protobuf field identified by @a path .
106
/// See @ref ConversionErrorInfo::ConversionErrorInfo for more information about @a path format.
107
ConversionError
(ErrorCodeType code, std::string path, std::string_view description =
""
);
108
109
/// @brief Returns information about occurred error.
110
[[
nodiscard
]]
const
ErrorInfoType&
GetErrorInfo
()
const
&
noexcept
{
return
error_info_; }
111
112
/// @brief Returns information about occurred error.
113
[[
nodiscard
]] ErrorInfoType
GetErrorInfo
() &&
noexcept
{
return
std::move(error_info_); }
114
115
private
:
116
ErrorInfoType error_info_;
117
};
118
119
extern
template
class
ConversionError<
ParseErrorCode
>;
120
extern
template
class
ConversionError<
PrintErrorCode
>;
121
122
/// @brief JSON `Value` to protobuf message conversion error.
123
using
ParseError
=
ConversionError
<
ParseErrorCode
>;
124
125
/// @brief Protobuf message to JSON `Value` conversion error.
126
using
PrintError
=
ConversionError
<
PrintErrorCode
>;
127
128
}
// namespace protobuf::json
129
130
USERVER_NAMESPACE_END
protobuf
include
userver
protobuf
json
exceptions.hpp
Generated on
for userver by
Doxygen
1.17.0