userver: /data/code/userver/libraries/protobuf/include/userver/protobuf/json/exceptions.hpp Source File
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
11USERVER_NAMESPACE_BEGIN
12
13/// @brief Top namespace for the protobuf JSON utilities.
14namespace protobuf::json {
15
16/// @brief JSON `Value` to protobuf message conversion error code.
17enum class ParseErrorCode {
18 /// @brief JSON field is unknown (does not match to any protobuf message field).
20
21 /// @brief Enum value name used as a JSON field value is unknown.
23
24 /// @brief JSON contains more than one field for the same oneof in the protobuf message.
26
27 /// @brief JSON field type is not compatible with corresponding protobuf message field type.
29
30 /// @brief JSON field value is invalid according to ProtoJSON rules.
32};
33
34/// @brief Protobuf message to JSON `Value` conversion error code.
35enum 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).
41};
42
43/// @brief JSON/protobuf conversion error information.
44/// @tparam TErrorCode error code type
45template <typename TErrorCode>
46requires(std::is_same_v<TErrorCode, ParseErrorCode> || std::is_same_v<TErrorCode, PrintErrorCode>)
48public:
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
74private:
75 ErrorCodeType code_;
76 std::string path_;
77};
78
79/// @brief JSON `Value` to protobuf message conversion error information.
81
82/// @brief Protobuf message to JSON `Value` conversion error information.
84
85/// @brief Base exception type for JSON utilities.
86class JsonError : public protobuf::Error {
87public:
88 using protobuf::Error::Error;
89};
90
91/// @brief Base exception type for JSON/protobuf conversion errors.
92class ConversionErrorBase : public protobuf::Error {
93public:
94 using protobuf::Error::Error;
95};
96
97/// @brief JSON/protobuf conversion error.
98/// @tparam TErrorCode error code type
99template <typename TErrorCode>
101public:
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
115private:
116 ErrorInfoType error_info_;
117};
118
119extern template class ConversionError<ParseErrorCode>;
120extern template class ConversionError<PrintErrorCode>;
121
122/// @brief JSON `Value` to protobuf message conversion error.
124
125/// @brief Protobuf message to JSON `Value` conversion error.
127
128} // namespace protobuf::json
129
130USERVER_NAMESPACE_END