diff options
| author | pechatnov <[email protected]> | 2026-03-26 10:11:59 +0300 |
|---|---|---|
| committer | pechatnov <[email protected]> | 2026-03-26 11:00:16 +0300 |
| commit | 9852998440dcaa3eec11377553e996ff9852bb4e (patch) | |
| tree | 83fd6cf6a491aba90ce28060576fdbf736939227 /library/cpp/yt/string/enum.cpp | |
| parent | 46c6fa262d8246e71e7f5ed33eb8458f45c5ddf7 (diff) | |
Add suggestions on enum parse error
commit_hash:3b7f35f613737aa92f473877e8b24782f7dfcd2a
Diffstat (limited to 'library/cpp/yt/string/enum.cpp')
| -rw-r--r-- | library/cpp/yt/string/enum.cpp | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/library/cpp/yt/string/enum.cpp b/library/cpp/yt/string/enum.cpp index 844254faa0f..beeba753daf 100644 --- a/library/cpp/yt/string/enum.cpp +++ b/library/cpp/yt/string/enum.cpp @@ -8,11 +8,39 @@ namespace NYT { namespace NDetail { -void ThrowMalformedEnumValueException(TStringBuf typeName, TStringBuf value) +//////////////////////////////////////////////////////////////////////////////// + +#if defined(_MSC_VER) + +extern "C" TEnumSuggestionsCalculator TryGetEnumSuggestionsCalculatorWeak() +{ + return nullptr; +} + +__pragma(comment(linker, "/alternatename:TryGetEnumSuggestionsCalculator=TryGetEnumSuggestionsCalculatorWeak")) + +#else + +extern "C" Y_WEAK TEnumSuggestionsCalculator TryGetEnumSuggestionsCalculator() { - throw TSimpleException(Format("Error parsing %v value %Qv", - typeName, - value)); + return nullptr; +} + +#endif + +//////////////////////////////////////////////////////////////////////////////// + +void ThrowMalformedEnumValueException( + TStringBuf typeName, + TStringBuf value, + const std::span<const TStringBuf>& domainNames) +{ + auto errorMessage = Format("Error parsing %v value %Qv", typeName, value); + auto suggestionsCalculator = TryGetEnumSuggestionsCalculator(); + if (!domainNames.empty() && suggestionsCalculator) { + errorMessage += Format("; closest possible values are %v", suggestionsCalculator(value, domainNames)); + } + throw TSimpleException(errorMessage); } template <bool ThrowOnError> @@ -32,8 +60,12 @@ std::optional<std::string> DecodeEnumValueImpl(TStringBuf value) return camelValue; } +//////////////////////////////////////////////////////////////////////////////// + } // namespace NDetail +//////////////////////////////////////////////////////////////////////////////// + std::optional<std::string> TryDecodeEnumValue(TStringBuf value) { return NDetail::DecodeEnumValueImpl<false>(value); |
