Skip to content

Commit fb7e5ba

Browse files
Add support for new Exception#detailed_message behaviour
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. There's two cases where the did_you_mean message is displayed, depending on whether we are in debug mode or not. * In debug mode, we let the original exception be raised. In this case, I think it's fine to let the new wording blow up, so I just changed the assertions on the Exception raised to comply with the new behaviour. * In non debug mode, we print a custom error. In this case, I slightly prefer the previous Thor output better than the one that `Exception#detailed_message` gives. The current one would be > Could not find command "paintz" in "barn" namespace. > Did you mean? "paint" vs the new one > Could not find command "paintz" in "barn" namespace. (Thor::UndefinedCommandError) > Did you mean? "paint" So I changed `Thor::Base` to manually build the message so that we keep the same error wording.
1 parent 5089c9b commit fb7e5ba

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/thor/base.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,16 @@ def start(given_args = ARGV, config = {})
484484
config[:shell] ||= Thor::Base.shell.new
485485
dispatch(nil, given_args.dup, nil, config)
486486
rescue Thor::Error => e
487-
config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
487+
if config[:debug] || ENV["THOR_DEBUG"] == "1"
488+
raise e
489+
else
490+
message = e.message
491+
extra_message = DidYouMean.formatter.message_for(e.corrections) if e.respond_to?(:corrections)
492+
if extra_message
493+
message << extra_message unless message.include?(extra_message)
494+
end
495+
config[:shell].error(message)
496+
end
488497
exit(false) if exit_on_failure?
489498
rescue Errno::EPIPE
490499
# This happens if a thor command is piped to something like `head`,

spec/parser/options_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,18 @@ def remaining
114114
parse("--bar", "baz", "--baz", "unknown")
115115

116116
expected = "Unknown switches \"--baz\""
117-
expected << "\nDid you mean? \"--bar\"" if Thor::Correctable
118117

119118
expect { check_unknown! }.to raise_error(Thor::UnknownArgumentError) do |error|
120-
expect(error.to_s).to eq(expected)
119+
expect(error.to_s).to include(expected)
120+
break unless Thor::Correctable
121+
122+
extra_expected = "\nDid you mean? \"--bar\""
123+
124+
if error.respond_to?(:detailed_message)
125+
expect(error.detailed_message(highlight: false)).to include(extra_expected)
126+
else
127+
expect(error.to_s).to include(extra_expected)
128+
end
121129
end
122130
end
123131

0 commit comments

Comments
 (0)