Suppressing the “Did you mean?” lines from exception message output.
When we enter one liners or other interactive code at the console, it can be helpful to have the “Did you mean?” lines output after the exception message. This feature is now on by default in Ruby 2.3+.
However, when you have code that outputs exception messages to $stderr
all the “Did you mean?” lines will just double the “noise” (or more if there are multiple suggestions.)
Below, is an example of suppressing these “Did you mean?” suggestion lines.
The test scenario uses the API Issue #31 where any untrapped exception occurring in an #respond_to?
overridden method in an observer object will (still as of SU2022) cause a BugSplat application crash.
So if you must override for testing or debugging, etc., be sure to trap exceptions in the overridden #respond_to?
of your observer(s).
You could use a special truncation method in your object …
def clean_message(err)
err.message.split(">\n").first # removes "Did you mean?" line(s)
end
But it turns out that the did_you_mean gem adds an extra #original_message
method to some of the Exception
subclasses. (Ie, those that mix in the DidYouMean::Correctable
module. The gem does this because it overrides the #message
method.)
class ModelSpy < Sketchup::ModelObserver
def self.attach
Sketchup.active_model.add_observer(self::new)
end
def respond_to?(meth)
# Mistyped puts() method name as Puts causes a NoMethodError ...
Puts "SketchUp polled #{self.class} for an \##{meth.to_s} callback."
rescue => err
if err.respond_to?(:original_message)
puts "\#<#{err.class.name}: #{err.original_message}.>"
else # did_you_mean gem is not loaded in pre2.3 Ruby
puts "\#<#{err.class.name}: #{err.message}.>"
end
puts "Error occurred in #{self.class.name}\##{__method__} when polling for an \##{meth.to_s} callback."
false
else
super(meth)
end
end
To test (in a SketchUp version running Ruby 2.3 or higher,) paste the above into the console, run ModelSpy.attach
, and then move an instance or somehow edit the model.
Looking at the Ruby docs for Kernel#puts
and Exception
class I did not see any method or extra argument to suppress these “Did you mean?” lines.
The only way I found it (via a Google search) was the README for the did_you_mean gem.
The gem is automatically loaded for Ruby 2.3+ and the only way to switch it off is by a command line switch when Ruby is loaded.