Skip to content

Commit e2aeb7e

Browse files
authored
Merge pull request #48 from zombocom/schneems/safer-no-method-error
Safer NoMethodError annotation
2 parents 22fd3a5 + a1e4263 commit e2aeb7e

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (unreleased)
22

3+
- Safer NoMethodError annotation (https://github.com/zombocom/dead_end/pull/48)
4+
35
## 1.1.0
46

57
- Annotate NoMethodError in non-production environments (https://github.com/zombocom/dead_end/pull/46)

lib/dead_end/auto.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ module DeadEnd
6262
# we can attempt to disable this behavior in a production context.
6363
if !DeadEnd::IsProduction.call
6464
class NoMethodError
65+
alias :original_to_s :to_s
66+
6567
def to_s
6668
return super if DeadEnd::IsProduction.call
6769

@@ -91,8 +93,9 @@ def to_s
9193
message << $/
9294
message
9395
rescue => e
94-
puts "DeadEnd Internal error: #{e.message}"
95-
puts "DeadEnd Internal backtrace: #{e.backtrace}"
96+
puts "DeadEnd Internal error: #{e.original_to_s}"
97+
puts "DeadEnd Internal backtrace:"
98+
puts backtrace.map {|l| " " + l }.join($/)
9699
super
97100
end
98101
end

spec/integration/ruby_command_line_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44

55
module DeadEnd
66
RSpec.describe "Requires with ruby cli" do
7+
it "does not get in an infinite loop when NoMethodError is raised internally" do
8+
Dir.mktmpdir do |dir|
9+
@tmpdir = Pathname(dir)
10+
@script = @tmpdir.join("script.rb")
11+
@script.write <<~'EOM'
12+
class DeadEnd::DisplayCodeWithLineNumbers
13+
def call
14+
raise NoMethodError.new("foo")
15+
end
16+
end
17+
18+
class Pet
19+
def initialize
20+
@name = "cinco"
21+
end
22+
23+
def call
24+
puts "Come here #{@neam.upcase}"
25+
end
26+
end
27+
28+
Pet.new.call
29+
EOM
30+
31+
out = `ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`
32+
33+
expect(out).to include("DeadEnd Internal error: foo")
34+
expect(out).to include("DeadEnd Internal backtrace")
35+
expect($?.success?).to be_falsey
36+
end
37+
end
38+
739
it "annotates NoMethodError" do
840
Dir.mktmpdir do |dir|
941
@tmpdir = Pathname(dir)

0 commit comments

Comments
 (0)