Skip to content

Commit 952003b

Browse files
committed
Handle exception in analyze, completion_candidates and doc_namespace
1 parent 343c0b7 commit 952003b

6 files changed

Lines changed: 65 additions & 19 deletions

File tree

bin/console

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ require "repl_completion"
1111
# require "pry"
1212
# Pry.start
1313

14+
ReplCompletion.preload_rbs
1415
require "irb"
1516
IRB.start(__FILE__)

lib/repl_completion.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module ReplCompletion
88
class << self
9-
attr_reader :last_analyze_error
9+
attr_reader :last_completion_error
1010

1111
def rbs_load_error
1212
Types.rbs_load_error
@@ -29,20 +29,18 @@ def preload_rbs
2929
end
3030

3131
def analyze(code, binding)
32-
preload_rbs
33-
begin
34-
verbose, $VERBOSE = $VERBOSE, nil
35-
result = analyze_code(code, binding)
36-
rescue Exception => e
37-
handle_error(e)
38-
ensure
39-
$VERBOSE = verbose
40-
end
32+
verbose, $VERBOSE = $VERBOSE, nil
33+
result = analyze_code(code, binding)
4134
Result.new(result, binding) if result
35+
rescue Exception => e
36+
handle_exception(e)
37+
nil
38+
ensure
39+
$VERBOSE = verbose
4240
end
4341

44-
def handle_error(e)
45-
@last_analyze_error = e
42+
def handle_exception(e)
43+
@last_completion_error = e
4644
end
4745

4846
def info

lib/repl_completion/result.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def completion_candidates
6666
[]
6767
end
6868
candidates.select { _1.start_with?(name) }.map { _1[name.size..] }
69+
rescue Exception => e
70+
ReplCompletion.handle_exception(e)
71+
[]
6972
ensure
7073
$VERBOSE = verbose
7174
end
@@ -97,6 +100,9 @@ def doc_namespace(matched)
97100
end
98101
else
99102
end
103+
rescue Exception => e
104+
ReplCompletion.handle_exception(e)
105+
nil
100106
ensure
101107
$VERBOSE = verbose
102108
end

sig/repl_completion.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ReplCompletion
99
def self.analyze: (String, Binding) -> Result?
1010

1111
def self.rbs_load_error: () -> Exception?
12-
def self.last_analyze_error: () -> Exception?
12+
def self.last_completion_error: () -> Exception?
1313

1414
def self.rbs_load_started?: () -> bool
1515
def self.rbs_loaded?: () -> bool

test/repl_completion/test_repl_completion.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,54 @@ def test_none
166166

167167
def test_repl_completion_api
168168
assert_nil ReplCompletion.rbs_load_error
169-
assert_nil ReplCompletion.last_analyze_error
169+
assert_nil ReplCompletion.last_completion_error
170170
assert_equal true, ReplCompletion.rbs_load_started?
171171
assert_equal true, ReplCompletion.rbs_loaded?
172172
assert_nothing_raised { ReplCompletion.preload_rbs }
173173
assert_nothing_raised { ReplCompletion.load_rbs }
174174
end
175175

176+
def with_failing_method(klass, method_name, message)
177+
original_method = klass.instance_method(method_name)
178+
klass.remove_method(method_name)
179+
klass.define_method(method_name) do |*, **|
180+
raise Exception.new(message)
181+
end
182+
yield
183+
ensure
184+
klass.remove_method(method_name)
185+
klass.define_method(method_name, original_method)
186+
end
187+
188+
def test_analyze_error
189+
with_failing_method(ReplCompletion.singleton_class, :analyze_code, 'error_in_analyze_code') do
190+
assert_nil ReplCompletion.analyze '1.', binding
191+
end
192+
assert_equal 'error_in_analyze_code', ReplCompletion.last_completion_error&.message
193+
ensure
194+
ReplCompletion.instance_variable_set(:@last_completion_error, nil)
195+
end
196+
197+
def test_completion_candidates_error
198+
result = ReplCompletion.analyze '1.', binding
199+
with_failing_method(ReplCompletion::Types::InstanceType, :methods, 'error_in_methods') do
200+
assert_equal [], result.completion_candidates
201+
end
202+
assert_equal 'error_in_methods', ReplCompletion.last_completion_error&.message
203+
ensure
204+
ReplCompletion.instance_variable_set(:@last_completion_error, nil)
205+
end
206+
207+
def test_doc_namespace_error
208+
result = ReplCompletion.analyze '1.', binding
209+
with_failing_method(ReplCompletion::Result, :method_doc, 'error_in_method_doc') do
210+
assert_nil result.doc_namespace('abs')
211+
end
212+
assert_equal 'error_in_method_doc', ReplCompletion.last_completion_error&.message
213+
ensure
214+
ReplCompletion.instance_variable_set(:@last_completion_error, nil)
215+
end
216+
176217
def test_info
177218
assert_equal "ReplCompletion: #{ReplCompletion::VERSION}, Prism: #{Prism::VERSION}, RBS: #{RBS::VERSION}", ReplCompletion.info
178219
end

test/repl_completion/test_type_analyze.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
module TestReplCompletion
77
class AnalyzeTest < TestCase
88
def setup
9-
@handle_error_method = ReplCompletion.method(:handle_error)
10-
ReplCompletion.singleton_class.remove_method(:handle_error)
11-
def ReplCompletion.handle_error(e)
9+
@handle_exception_method = ReplCompletion.method(:handle_exception)
10+
ReplCompletion.singleton_class.remove_method(:handle_exception)
11+
def ReplCompletion.handle_exception(e)
1212
raise e
1313
end
1414
ReplCompletion::Types.load_rbs_builder unless ReplCompletion::Types.rbs_builder
1515
end
1616

1717
def teardown
18-
ReplCompletion.singleton_class.remove_method(:handle_error)
19-
ReplCompletion.define_singleton_method(:handle_error, &@handle_error_method)
18+
ReplCompletion.singleton_class.remove_method(:handle_exception)
19+
ReplCompletion.define_singleton_method(:handle_exception, &@handle_exception_method)
2020
end
2121

2222
def empty_binding

0 commit comments

Comments
 (0)