Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception Hints #302

Merged
merged 3 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/better_errors/raised_exception.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @private
module BetterErrors
class RaisedException
attr_reader :exception, :message, :backtrace
attr_reader :exception, :message, :backtrace, :hint

def initialize(exception)
if exception.respond_to?(:original_exception) && exception.original_exception
Expand All @@ -12,6 +12,7 @@ def initialize(exception)
@message = exception.message

setup_backtrace
setup_hint
massage_syntax_error
end

Expand Down Expand Up @@ -62,5 +63,29 @@ def massage_syntax_error
end
end
end

def setup_hint
case exception
when NoMethodError
matches = /\Aundefined method `([^']+)' for ([^:]+):(\w+)\z/.match(message)
if matches
method = matches[1]
val = matches[2]
klass = matches[3]

if val == "nil"
@hint = "Something is `nil` when it probably shouldn't be."
else
@hint = "`#{method}` is being called on a `#{klass}`, which probably isn't the type you were expecting."
end
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
end
9 changes: 6 additions & 3 deletions lib/better_errors/templates/main.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
nav.sidebar,
.frame_info {
position: fixed;
top: 95px;
top: 102px;
bottom: 0;

box-sizing: border-box;
Expand All @@ -102,7 +102,7 @@
nav.sidebar {
width: 40%;
left: 20px;
top: 115px;
top: 122px;
bottom: 20px;
}

Expand Down Expand Up @@ -131,7 +131,7 @@
header.exception {
padding: 18px 20px;

height: 59px;
height: 66px;
min-height: 59px;

overflow: hidden;
Expand Down Expand Up @@ -733,6 +733,9 @@
<header class="exception">
<h2><strong><%= exception.type %></strong> <span>at <%= request_path %></span></h2>
<p><%= exception.message %></p>
<% if exception.hint %>
<h2>Hint: <%= exception.hint %></h2>
<% end %>
</header>
</div>

Expand Down
37 changes: 37 additions & 0 deletions spec/better_errors/raised_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,42 @@ module BetterErrors
subject.backtrace.first.line.should == 11
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
val.foo
rescue NoMethodError => e
e
end
}

context "on `nil`" do
let(:val) { nil }

its(:type) { should == NoMethodError }
its(:hint) { should == "Something is `nil` when it probably shouldn't be." }
end

context "on other values" do
let(:val) { 42 }

its(:type) { should == NoMethodError }
its(:hint) { should == "`foo` is being called on a `Fixnum`, which probably isn't the type you were expecting." }
end
end
end
end