-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MatchData: improve error message when refer no matched existed capture group #4553
MatchData: improve error message when refer no matched existed capture group #4553
Conversation
…e group For example, `"foo".match(/(?<g1>x)?foo/)["g1"]` raises an exception with message `"Match group 'g1' does not exist"`, bu group `"g1"` exists surely and it isn't matched on this matching. I think we can improve such an error message.
src/regex/match_data.cr
Outdated
if ret < 0 | ||
raise KeyError.new("Capture group named '#{group_name}' does not exist") | ||
else | ||
raise KeyError.new("Capture group named '#{group_name}' is not matched") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"was not matched" sounds better
src/regex/match_data.cr
Outdated
@@ -315,5 +320,9 @@ class Regex | |||
private def raise_invalid_group_index(index) | |||
raise IndexError.new("Invalid capture group index: #{index}") | |||
end | |||
|
|||
private def raise_not_matched_group_index(index) | |||
raise IndexError.new("Not matched capture group index: #{index}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Capture group index was not matched: #{index}"
?
@makenowjust I agree with @RX14 , let's use:
|
@asterite Ok. I'll fix it. |
@makenowjust I think it's fine like that |
@asterite Thank you ❤️ |
src/regex/match_data.cr
Outdated
raise KeyError.new("Capture group named '#{group_name}' is not matched") | ||
end | ||
status = ret < 0 ? "does not exist" : "was not matched" | ||
raise KeyError.new("Capture group '#{group_name}' #{status}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having 2 if branches here is much cleaner. Removing duplication isn't always good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use 80 column terminal window always, so this line is hard to read if if
nested. It's only reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's little problem, so I fixed it.
c758745
to
de1c217
Compare
For example,
"foo".match(/(?<g1>x)?foo/).not_nil!["g1"]
raises an exception with message"Match group 'g1' does not exist"
, but group"g1"
exists surely and it isn't matched on this matching. I think we can improve such an error message.