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, ReadErrorCode>) {
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 switch (code) {
32 case TErrorCode::kInvalidValue:
33 return "invalid value";
34 default:
35 return "unexpected";
36 }
37 }
38}
39
40template <typename TErrorCode>
41[[nodiscard]] constexpr const char* GetConversionErrorDescription() noexcept {
42 if constexpr (std::is_same_v<TErrorCode, ReadErrorCode>) {
43 return "Failed to convert JSON field '{}' with error '{}'";
44 } else {
45 return "Failed to convert protobuf message field '{}' with error '{}'";
46 }
47}
48
49} // namespace
50
51template <typename TErrorCode>
55 ),
57{}
58
59template class ConversionError<ReadErrorCode>;
60template class ConversionError<WriteErrorCode>;
61
62} // namespace protobuf::json
63
64USERVER_NAMESPACE_END