Skip to content
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

Conversation

makenowjust
Copy link
Contributor

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.

makenowjust and others added 2 commits June 12, 2017 23:03
…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.
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")
Copy link
Contributor

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

@@ -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}")
Copy link
Contributor

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}"?

@asterite
Copy link
Member

@makenowjust I agree with @RX14 , let's use:

  • "Capture group '#{name}' was not matched" in line 164
  • "Capture group #{index} was not matched" in line 325

@makenowjust
Copy link
Contributor Author

@asterite Ok. I'll fix it.

@makenowjust
Copy link
Contributor Author

makenowjust commented Jun 13, 2017

@asterite @RX14 Should is "Invalid capture group index: #{index}" changed?

@asterite
Copy link
Member

@makenowjust I think it's fine like that

@makenowjust
Copy link
Contributor Author

@asterite Thank you ❤️

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}")
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@makenowjust makenowjust force-pushed the fix/regex-match-data/improve-error-message branch from c758745 to de1c217 Compare June 13, 2017 13:44
@asterite asterite added this to the Next milestone Jun 13, 2017
@asterite asterite merged commit 1a71e3d into crystal-lang:master Jun 13, 2017
@makenowjust makenowjust deleted the fix/regex-match-data/improve-error-message branch June 13, 2017 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants