Skip to content

Commit

Permalink
Show file and line within ActionView template
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDaugherty committed May 13, 2020
1 parent 4703817 commit 73c02ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/better_errors/raised_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def setup_backtrace_from_backtrace

def massage_syntax_error
case exception.class.to_s
when "ActionView::Template::Error"
if exception.respond_to?(:file_name) && exception.respond_to?(:line_number)
backtrace.unshift(StackFrame.new(exception.file_name, exception.line_number.to_i, "view template"))
end
when "Haml::SyntaxError", "Sprockets::Coffeelint::Error"
if /\A(.+?):(\d+)/ =~ exception.backtrace.first
backtrace.unshift(StackFrame.new($1, $2.to_i, ""))
Expand Down
31 changes: 30 additions & 1 deletion spec/better_errors/raised_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module BetterErrors
its(:message) { is_expected.to eq "something went wrong!" }
end

context "when the exception is a syntax error" do
context "when the exception is a SyntaxError" do
let(:exception) { SyntaxError.new("foo.rb:123: you made a typo!") }

its(:message) { is_expected.to eq "you made a typo!" }
Expand Down Expand Up @@ -50,6 +50,35 @@ module BetterErrors
end
end

context "when the exception is an ActionView::Template::Error" do
before do
stub_const(
"ActionView::Template::Error",
Class.new(StandardError) do
def file_name
"app/views/foo/bar.haml"
end

def line_number
42
end
end
)
end

let(:exception) {
ActionView::Template::Error.new("undefined method `something!' for #<Class:0x00deadbeef>")
}

its(:message) { is_expected.to eq "undefined method `something!' for #<Class:0x00deadbeef>" }
its(:type) { is_expected.to eq ActionView::Template::Error }

it "has the right filename and line number in the backtrace" do
expect(subject.backtrace.first.filename).to eq("app/views/foo/bar.haml")
expect(subject.backtrace.first.line).to eq(42)
end
end

context "when the exception is a Coffeelint syntax error" do
before do
stub_const("Sprockets::Coffeelint::Error", Class.new(SyntaxError))
Expand Down

0 comments on commit 73c02ed

Please sign in to comment.