1#include <userver/protobuf/json/exceptions.hpp>
9namespace protobuf::json {
13template <
typename TErrorCode>
14[[nodiscard]]
constexpr const char* GetConversionErrorCodeStr(
const TErrorCode code)
noexcept {
15 if constexpr (std::is_same_v<TErrorCode, ParseErrorCode>) {
17 case TErrorCode::kUnknownField:
18 return "unknown field";
19 case TErrorCode::kUnknownEnum:
20 return "unknown enum";
21 case TErrorCode::kMultipleOneofFields:
22 return "multiple oneof fields";
23 case TErrorCode::kInvalidType:
24 return "invalid type";
25 case TErrorCode::kInvalidValue:
26 return "invalid value";
31 if (code == TErrorCode::kInvalidValue) {
32 return "invalid value";
39template <
typename TErrorCode>
40[[nodiscard]]
constexpr const char* GetConversionErrorDescription(
bool with_description)
noexcept {
41 if constexpr (std::is_same_v<TErrorCode, ParseErrorCode>) {
42 return with_description
43 ?
"Failed to convert JSON field '{}' with error '{}' ({})"
44 :
"Failed to convert JSON field '{}' with error '{}'";
46 return with_description
47 ?
"Failed to convert protobuf message field '{}' with error '{}' ({})"
48 :
"Failed to convert protobuf message field '{}' with error '{}'";
54template <
typename TErrorCode>
55ConversionError<TErrorCode>::ConversionError(
56 const ConversionError<TErrorCode>::ErrorCodeType code,
58 std::string_view description
60 : ConversionErrorBase(
62 ? fmt::format(GetConversionErrorDescription<TErrorCode>(
false), path, GetConversionErrorCodeStr(code))
64 GetConversionErrorDescription<TErrorCode>(
true),
66 GetConversionErrorCodeStr(code),
70 error_info_(code, std::move(path))
73template class ConversionError<ParseErrorCode>;
74template class ConversionError<PrintErrorCode>;