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 a line in application code instead of gems #35

Open
mame opened this issue Mar 2, 2023 · 1 comment · May be fixed by #54
Open

Display a line in application code instead of gems #35

mame opened this issue Mar 2, 2023 · 1 comment · May be fixed by #54

Comments

@mame
Copy link
Member

mame commented Mar 2, 2023

Currently, error_highlight spots the deepest Ruby frame. This may show code in a gem, which may not be very useful for application writers.

Consider:

# gem code
class SomeGem
  def foo(x)
    raise ArgumentError, "wrong input" if x == nil
  end
end

# app code
SomeGem.new.foo(nil)

Here is the current behavior:

$ ruby test.rb
test.rb:4:in `foo': wrong input (ArgumentError)

    raise ArgumentError, "wrong input" if x == nil
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        from test.rb:9:in `<main>'

However, application writers may want to see the line of SomeGem.new.foo(nil).

How might this be resolved?

  1. Display code snippets for all frames, not just for the deepest one. Python does this, but I am not very keen on it because it makes the error trace very messy.
  2. Add an argument Kernel#raise to tell error_highlight which frame should be displayed, for example, raise ArgumentError, "wrong input", error_highlight_skip_frames: 1.

For 2, there are two ways how to show the error.

2-1. Keep the error trace as is and change only the snippet. This might be confusing because the snippet line does not exist in test.rb:4.

$ ruby test.rb
test.rb:4:in `foo': wrong input (ArgumentError)

SomeGem.new.foo(nil)
                ^^^
        from test.rb:9:in `<main>'

2-2. Skip the error trace. This looks good to me, but I think this is beyond the responsibilities of error_highlight.

$ ruby test.rb
test.rb:9:in `<main>': wrong input (ArgumentError)

SomeGem.new.foo(nil)
                ^^^

I wonder if we should put something like raise skip_frames: n in the Ruby core side? I need to organize my thoughts.

@karreiro
Copy link
Contributor

Hey @mame, the second approach looks cleaner indeed. Based on that, I’ve put together a proposal for skipping lines in gem files: #54. Do you have any thoughts on this approach?

It’s still a work in progress (I’m looking into the Prism failure), but I’d really appreciate your thoughts on this direction when you have a moment. Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants