From fd6122add51bbfd0ffb4404740fc31c4414b1483 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Fri, 19 Jul 2024 01:25:38 -0400 Subject: [PATCH] Make the error codes and messages more descriptive --- .../core/src/clp/regex_utils/ErrorCode.cpp | 28 +++++++++++-------- .../core/src/clp/regex_utils/ErrorCode.hpp | 17 ++++------- .../regex_utils/regex_translation_utils.cpp | 12 ++++---- components/core/tests/test-regex_utils.cpp | 12 ++++---- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/components/core/src/clp/regex_utils/ErrorCode.cpp b/components/core/src/clp/regex_utils/ErrorCode.cpp index d899ae25b..de3480ed8 100644 --- a/components/core/src/clp/regex_utils/ErrorCode.cpp +++ b/components/core/src/clp/regex_utils/ErrorCode.cpp @@ -40,24 +40,28 @@ auto ErrorCodeCategory::message(int ev) const -> string { case ErrorCode::IllegalState: return "Unrecognized state."; - case ErrorCode::Star: - return "Failed to translate due to metachar `*` (zero or more occurences)."; + case ErrorCode::UntranslatableStar: + return "Unable to express regex quantifier `*` in wildcard, which repeats a token for " + "zero or more occurences, unless it is combined with a wildcard `.`"; - case ErrorCode::Plus: - return "Failed to translate due to metachar `+` (one or more occurences)."; + case ErrorCode::UntranslatablePlus: + return "Unable to express regex quantifier `+` in wildcard, which repeats a token for " + "one or more occurences, unless it is combined with a wildcard `.`"; - case ErrorCode::Question: - return "Currently does not support returning a list of wildcard translations. The " - "metachar `?` (lazy match) may be supported in the future."; + case ErrorCode::UnsupportedQuestionMark: + return "Unable to express regex quantifier `?` in wildcard, which makes the preceding " + "token optional, unless the translator supports returning a list of possible " + "wildcard translations."; - case ErrorCode::Pipe: - return "Currently does not support returning a list of wildcard translations. The " - "regex OR condition feature may be supported in the future."; + case ErrorCode::UnsupportedPipe: + return "Unable to express regex OR `|` in wildcard, which allows the query string to " + "match a single token out of a series of options, unless the translator " + "supports returning a list of possible wildcard translations."; - case ErrorCode::Caret: + case ErrorCode::IllegalCaret: return "Failed to translate due to start anchor `^` in the middle of the string."; - case ErrorCode::Dollar: + case ErrorCode::IllegalDollarSign: return "Failed to translate due to end anchor `$` in the middle of the string."; case ErrorCode::UnmatchedParenthesis: diff --git a/components/core/src/clp/regex_utils/ErrorCode.hpp b/components/core/src/clp/regex_utils/ErrorCode.hpp index acb1dcc37..1babb2fec 100644 --- a/components/core/src/clp/regex_utils/ErrorCode.hpp +++ b/components/core/src/clp/regex_utils/ErrorCode.hpp @@ -13,18 +13,13 @@ namespace clp::regex_utils { enum class ErrorCode : uint8_t { Success = 0, IllegalState, - Star, - Plus, - Question, - Pipe, - Caret, - Dollar, - DisallowedEscapeSequence, + UntranslatableStar, + UntranslatablePlus, + UnsupportedQuestionMark, + UnsupportedPipe, + IllegalCaret, + IllegalDollarSign, UnmatchedParenthesis, - UnsupportedCharsets, - IncompleteCharsetStructure, - UnsupportedQuantifier, - TokenUnquantifiable, }; /** diff --git a/components/core/src/clp/regex_utils/regex_translation_utils.cpp b/components/core/src/clp/regex_utils/regex_translation_utils.cpp index 372629639..7ba014d73 100644 --- a/components/core/src/clp/regex_utils/regex_translation_utils.cpp +++ b/components/core/src/clp/regex_utils/regex_translation_utils.cpp @@ -150,15 +150,15 @@ auto normal_state_transition( state.set_next_state(TranslatorState::RegexPatternState::END); break; case cRegexZeroOrMore: - return ErrorCode::Star; + return ErrorCode::UntranslatableStar; case cRegexOneOrMore: - return ErrorCode::Plus; + return ErrorCode::UntranslatablePlus; case cRegexZeroOrOne: - return ErrorCode::Question; + return ErrorCode::UnsupportedQuestionMark; case '|': - return ErrorCode::Pipe; + return ErrorCode::UnsupportedPipe; case cRegexStartAnchor: - return ErrorCode::Caret; + return ErrorCode::IllegalCaret; case ')': return ErrorCode::UnmatchedParenthesis; default: @@ -209,7 +209,7 @@ auto end_state_transition( [[maybe_unused]] RegexToWildcardTranslatorConfig const& config ) -> error_code { if (cRegexEndAnchor != *it) { - return ErrorCode::Dollar; + return ErrorCode::IllegalDollarSign; } return ErrorCode::Success; } diff --git a/components/core/tests/test-regex_utils.cpp b/components/core/tests/test-regex_utils.cpp index 1f0f954a8..080e00238 100644 --- a/components/core/tests/test-regex_utils.cpp +++ b/components/core/tests/test-regex_utils.cpp @@ -18,11 +18,11 @@ TEST_CASE("regex_to_wildcard", "[regex_utils][regex_to_wildcard]") { REQUIRE((regex_to_wildcard(". xyz .+ zyx .*").value() == "? xyz ?* zyx *")); // Test unescaped meta characters - REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::Question)); - REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::Star)); - REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::Plus)); - REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::Pipe)); - REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::Caret)); + REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::UnsupportedQuestionMark)); + REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::UntranslatableStar)); + REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::UntranslatablePlus)); + REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::UnsupportedPipe)); + REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::IllegalCaret)); } // Test anchors and prefix/suffix wildcards @@ -34,5 +34,5 @@ TEST_CASE("regex_to_wildcard_anchor_config", "[regex_utils][regex_to_wildcard][a REQUIRE((regex_to_wildcard("xyz", config).value() == "*xyz*")); REQUIRE((regex_to_wildcard("xyz$$", config).value() == "*xyz")); - REQUIRE((regex_to_wildcard("xyz$zyx$", config).error() == ErrorCode::Dollar)); + REQUIRE((regex_to_wildcard("xyz$zyx$", config).error() == ErrorCode::IllegalDollarSign)); }