Skip to content

Commit

Permalink
give hints for NameErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Apr 5, 2015
1 parent 890c83f commit 56340ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/better_errors/raised_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def massage_syntax_error
end

def setup_hint
if exception.is_a?(NoMethodError)
case exception
when NoMethodError
matches = /\Aundefined method `([^']+)' for ([^:]+):(\w+)\z/.match(message)
method = matches[1]
val = matches[2]
Expand All @@ -76,6 +77,12 @@ def setup_hint
else
@hint = "`#{method}` is being called on a `#{klass}`, which probably isn't the type you were expecting."
end
when NameError
matches = /\Aundefined local variable or method `([^']+)' for/.match(message)
if matches
method_or_var = matches[1]
@hint = "`#{method_or_var}` is probably misspelled."
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/better_errors/raised_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ module BetterErrors
end
end

context "when the exception is a NameError" do
let(:exception) {
begin
foo
rescue NameError => e
e
end
}

its(:type) { should == NameError }
its(:hint) { should == "`foo` is probably misspelled." }
end

context "when the exception is a NoMethodError" do
let(:exception) {
begin
Expand Down

0 comments on commit 56340ab

Please sign in to comment.