Skip to content

Commit da964d8

Browse files
committed
When a Thor command doesn't exist, print command not found instead of
throwing an exception
1 parent c46874f commit da964d8

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

gem/terminalwire-rails/lib/terminalwire/rails.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,15 @@ def handle(adapter:, env:)
9090
case message
9191
in { event: "initialization", protocol:, program: { arguments: }, entitlement: }
9292
context = Terminalwire::Server::Context.new(adapter:, entitlement:)
93+
exit_code = 0
9394

9495
begin
9596
@cli_class.terminalwire arguments:, context: do |cli|
9697
cli.default_url_options = { host: env["HTTP_HOST"] }
9798
end
98-
context.exit
99-
# rescue Thor::InvocationError => e
100-
# context.stdin.puts e.message
101-
# context.exit(1)
102-
rescue StandardError => e
99+
rescue ::Thor::UndefinedCommandError => e
100+
context.stdout.puts e.message
101+
rescue ::StandardError => e
103102
# Log the error
104103
handler_error_message = <<~_
105104
An error occured handling message in #{self.class.name}: #{e.inspect}
@@ -120,7 +119,9 @@ def handle(adapter:, env:)
120119
# Show a generic message in production
121120
context.stderr.puts error_message
122121
end
123-
context.exit 1
122+
exit_code = 1
123+
ensure
124+
context.exit exit_code
124125
end
125126
end
126127
end
@@ -158,10 +159,9 @@ def self.included(base)
158159
end
159160

160161
module ClassMethods
161-
def terminalwire(arguments:, context:)
162-
dispatch(nil, arguments.dup, nil, shell: Shell.new(context)) do |instance|
163-
yield instance if block_given?
164-
end
162+
# Use the extend Rails shell with this shell
163+
def terminalwire_shell(context)
164+
Terminalwire::Thor::Shell.new(context)
165165
end
166166
end
167167
end

gem/terminalwire-server/lib/terminalwire/server/thor.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ def fail(...)
5757
end
5858

5959
module ClassMethods
60-
def start(given_args = ARGV, config = {})
61-
context = config.delete(:context)
62-
config[:shell] = Shell.new(context) if context
63-
super(given_args, config)
60+
def terminalwire(arguments:, context:)
61+
# I have to manually hack into the Thor dispatcher to get access to the instance
62+
# of the CLI so I can slap the Rails helper methods in there, or other helpes
63+
# raise [context.inspect, arguments.inspect, self.inspect].inspect
64+
dispatch(nil, arguments.dup, nil, shell: terminalwire_shell(context)) do |instance|
65+
yield instance
66+
end
67+
end
68+
69+
def terminalwire_shell(context)
70+
Terminalwire::Server::Thor::Shell.new(context)
6471
end
6572
end
6673
end

spec/integration/rails_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def console(&)
4949
end
5050
end
5151

52+
it "runs unknown commands" do
53+
console do |repl|
54+
repl.puts "#{BINARY_NAME} nothingburger"
55+
repl.gets.tap do |buffer|
56+
expect(buffer).to include("Could not find command \"nothingburger\".")
57+
expect(buffer).to_not include("Thor::UndefinedCommandError")
58+
end
59+
end
60+
end
61+
5262
private
5363

5464
def wait_for_server_in_container(timeout:)

0 commit comments

Comments
 (0)