Skip to content

Question: understanding strings appended to NoMethodError message upon raise #20

@searls

Description

@searls

Hello @mame and friends! Thank you for this gem and for working to improve the error experience in Ruby 3.1

I have a few gems that instantiate or extend built-in error types other than StandardError, often to imitate default behavior when metaprogramming. Under Ruby 3.1, Mocktail's test suite started failing because of changes introduced by the error_highlight gem. Specifically, I'm working on fixing this test at the moment.

To verify this is related to the error_highlight gem, I confirmed that this passes:

$ RUBYLIB='./lib:./test' ruby --disable-error_highlight test/unit/raises_neato_no_method_error_test.rb

It seems that this affects NoMethodError objects I create but not StandardError (I haven't tested other types), and only after they have been raised. Here is a minimal example of my issue:

begin
  e = StandardError.new("pants")
  puts "StandardError message BEFORE raise: #{e.message}"
  raise e
rescue => e
  puts "StandardError message AFTER raise: #{e.message}\n\n"
end

begin
  e = NoMethodError.new("pants")
  puts "NoMethodError message BEFORE raise: #{e.message}"
  raise e
rescue => e
  puts "NoMethodError message AFTER raise: #{e.message}"
end

This will output:

StandardError message BEFORE raise: pants
StandardError message AFTER raise: pants

NoMethodError message BEFORE raise: pants
NoMethodError message AFTER raise: pants

  raise e
  ^^^^^

Because I am instantiating these errors myself in library code, appending their raise line (which is inside my gem) is confusing and not helpful to the user.

As a result, I am wondering:

  • Should I stop raising built-in error types like NoMethodError myself?
  • Is there an API I don't know about that will allow me to prevent these ^^^^^ lines from being appended when the error is raised? Like an argument I can pass to its constructor or a property I can set?
  • Should error classes or this gem expose an API that allows temporary suppression of these changes to error messages, to support cases where library and framework code are creating them artificially?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions