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

Display all syntax error messages when catching SyntaxException #2117

Merged
merged 1 commit into from Mar 21, 2020

Conversation

barrettkingram
Copy link
Contributor

Previously when catching syntax errors in the REPL, we were only showing
the last syntax error displayed by the ruby output. However, ruby can
generate multiple syntax error messages within a single SyntaxException.
For example, this code generates multiple syntax error messages:

$ ruby -e 'puts {"key"=>"val"}.to_json'
-e:1: syntax error, unexpected =>, expecting '}'
puts {"key"=>"val"}.to_json
-e:1: syntax error, unexpected '}', expecting end-of-input
puts {"key"=>"val"}.to_json

We can't predict which error message would be most helpful for the
consumer - we should show both of them.

This PR modifies the string replacement we're doing when printing
syntax exceptions so any number of syntax error lines will be shown
correctly.

Previous behavior:

[1] pry(main)> puts {"key"=>"val"}.to_json
SyntaxError: unexpected '}', expecting end-of-input
puts {"key"=>"val"}.to_json
                  ^
[1] pry(main)>

New behavior:

[1] pry(main)> puts {"key"=>"val"}.to_json
SyntaxError: unexpected =>, expecting '}'
puts {"key"=>"val"}.to_json
           ^~
SyntaxError: unexpected '}', expecting end-of-input
puts {"key"=>"val"}.to_json
                  ^
[1] pry(main)>

Issue: #2102
The error message of SyntaxError is different from Ruby's one

Previously when catching syntax errors in the REPL, we were only showing
the last syntax error displayed by the ruby output. However, ruby can
generate multiple syntax error messages within a single SyntaxException.
For example, this code generates multiple syntax error messages:
```
$ ruby -e 'puts {"key"=>"val"}.to_json'
-e:1: syntax error, unexpected =>, expecting '}'
puts {"key"=>"val"}.to_json
-e:1: syntax error, unexpected '}', expecting end-of-input
puts {"key"=>"val"}.to_json
```
We can't predict which error message would be most helpful for the
consumer - we should show both of them.

This commit modifies the string replacement we're doing when printing
SyntaxExceptions so any number of syntax error lines will be shown
correctly.

Issue: pry#2102
The error message of SyntaxError is different from Ruby's one
Copy link
Member

@kyrylo kyrylo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good stuff

@kyrylo kyrylo merged commit e9ad447 into pry:master Mar 21, 2020
kyrylo added a commit that referenced this pull request Mar 21, 2020
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Apr 27, 2020
Update ruby-pry to 0.13.1.


### [v0.13.1][v0.13.1] (April 12, 2020)

#### Bug fixes

* Fixed bug where on invalid input only the last syntax error is displayed
  (instead of all of them) ([#2117](pry/pry#2117))
* Fixed `Pry::Config` raising `NoMethodError` on undefined option instead of
  returning `nil` (usually invoked via `Pry.config.foo_option` calls)
  ([#2126](pry/pry#2126))
* Fixed `help` command not displaying regexp aliases properly
  ([#2120](pry/pry#2120))
* Fixed `pry-backtrace` not working ([#2122](pry/pry#2122))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants