Skip to content

Commit 46d1422

Browse files
Reimplement did_you_mean suggestions to keep behaviour accross rubies
Ruby 3.2 will introduce `Exception#detailed_message` and `did_you_mean` has been already updated in Ruby 3.2 to use that. The new behaviour means not changing the original `Exception#message`. That means it is hard to get the previous error output, because `Exception#detailed_message` includes not only `did_you_mean` decorations, but also additional information like the exception class. To fix this, I bring the old did_you_mean behavior into Thor, so that the above changes do not affect us.
1 parent 5089c9b commit 46d1422

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

lib/thor/error.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ def initialize(dictionary)
1111
end
1212
end
1313

14-
DidYouMean::Correctable
14+
Module.new do
15+
def to_s
16+
super + DidYouMean.formatter.message_for(corrections)
17+
end
18+
19+
def corrections
20+
@corrections ||= self.class.const_get(:SpellChecker).new(self).corrections
21+
end
22+
end
1523
end
1624

1725
# Thor::Error is raised when it's caused by wrong usage of thor classes. Those
@@ -100,16 +108,4 @@ class RequiredArgumentMissingError < InvocationError
100108

101109
class MalformattedArgumentError < InvocationError
102110
end
103-
104-
if Correctable
105-
if DidYouMean.respond_to?(:correct_error)
106-
DidYouMean.correct_error(Thor::UndefinedCommandError, UndefinedCommandError::SpellChecker)
107-
DidYouMean.correct_error(Thor::UnknownArgumentError, UnknownArgumentError::SpellChecker)
108-
else
109-
DidYouMean::SPELL_CHECKERS.merge!(
110-
'Thor::UndefinedCommandError' => UndefinedCommandError::SpellChecker,
111-
'Thor::UnknownArgumentError' => UnknownArgumentError::SpellChecker
112-
)
113-
end
114-
end
115111
end

0 commit comments

Comments
 (0)