userver: /data/code/userver/libraries/protobuf/src/protobuf/json/exceptions.cpp Source File
Loading...
Searching...
No Matches
exceptions.cpp
1#include <userver/protobuf/json/exceptions.hpp>
2
3#include <type_traits>
4
5#include <fmt/format.h>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace protobuf::json {
10
11namespace {
12
13template <typename TErrorCode>
14[[nodiscard]] constexpr const char* GetConversionErrorCodeStr(const TErrorCode code) noexcept {
15 if constexpr (std::is_same_v<TErrorCode, ParseErrorCode>) {
16 switch (code) {
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";
27 default:
28 return "unexpected";
29 }
30 } else {
31 if (code == TErrorCode::kInvalidValue) {
32 return "invalid value";
33 } else {
34 return "unexpected";
35 }
36 }
37}
38
39template <typename TErrorCode>
40[[nodiscard]] constexpr const char* GetConversionErrorDescription() noexcept {
41 if constexpr (std::is_same_v<TErrorCode, ParseErrorCode>) {
42 return "Failed to convert JSON field '{}' with error '{}'";
43 } else {
44 return "Failed to convert protobuf message field '{}' with error '{}'";
45 }
46}
47
48} // namespace
49
50template <typename TErrorCode>
51ConversionError<TErrorCode>::ConversionError(const ConversionError<TErrorCode>::ErrorCodeType code, std::string path)
52 : ConversionErrorBase(
53 fmt::format(GetConversionErrorDescription<TErrorCode>(), path, GetConversionErrorCodeStr(code))
54 ),
55 error_info_(code, std::move(path))
56{}
57
58template class ConversionError<ParseErrorCode>;
59template class ConversionError<PrintErrorCode>;
60
61} // namespace protobuf::json
62
63USERVER_NAMESPACE_END