Skip to content

Commit

Permalink
Merge pull request rails#13890 from achempion/syntax-error-backtrace
Browse files Browse the repository at this point in the history
append link to bad code when error type is SyntaxError
  • Loading branch information
senny committed Mar 29, 2014
2 parents 0b33c06 + 6af07c2 commit 03bf81a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Append link to bad code to backtrace when exception is SyntaxError.

*Boris Kuznetsov*

* Swapped the parameters of assert_equal in `assert_select` so that the
proper values were printed correctly

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ExceptionWrapper
def initialize(env, exception)
@env = env
@exception = original_exception(exception)

expand_backtrace if exception.is_a?(SyntaxError) || exception.try(:original_exception).try(:is_a?, SyntaxError)
end

def rescue_template
Expand Down Expand Up @@ -104,5 +106,11 @@ def source_fragment(path, line)
end
end
end

def expand_backtrace
@exception.backtrace.unshift(
@exception.to_s.split("\n")
).flatten!
end
end
end
35 changes: 35 additions & 0 deletions actionpack/test/dispatch/debug_exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def call(env)
raise ActionController::UrlGenerationError, "No route matches"
when "/parameter_missing"
raise ActionController::ParameterMissing, :missing_param_key
when "/original_syntax_error"
eval 'broke_syntax =' # `eval` need for raise native SyntaxError at runtime
when "/syntax_error_into_view"
begin
eval 'broke_syntax ='
rescue Exception => e
template = ActionView::Template.new(File.read(__FILE__),
__FILE__,
ActionView::Template::Handlers::Raw.new,
{})
raise ActionView::Template::Error.new(template, e)
end

else
raise "puke!"
end
Expand Down Expand Up @@ -242,4 +255,26 @@ def setup
get "/", {}, env
assert_operator((output.rewind && output.read).lines.count, :>, 10)
end

test 'display backtrace when error type is SyntaxError' do
@app = DevelopmentApp

get '/original_syntax_error', {}, {'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new}

assert_response 500
assert_select '#Application-Trace' do
assert_select 'pre code', /\(eval\):1: syntax error, unexpected/
end
end

test 'display backtrace when error type is SyntaxError wrapped by ActionView::Template::Error' do
@app = DevelopmentApp

get '/syntax_error_into_view', {}, {'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new}

assert_response 500
assert_select '#Application-Trace' do
assert_select 'pre code', /\(eval\):1: syntax error, unexpected/
end
end
end

0 comments on commit 03bf81a

Please sign in to comment.