Skip to content

Commit

Permalink
Addressed an issue where syntax errors generated inside an eval
Browse files Browse the repository at this point in the history
method cause an Internal Server Error due to a TypeError.

In order to display the extraced source of a syntax error, we try
to locate the node id for the backtrace location. The call to
RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location is expecting
a Thread::Backtrace::Location object, but we are passing a
SourceMapLocation instead.

This commit does two things:
1) it addresses the issue by making sure that we are always passing
a Thread::Backtrace::Location instead
2) it allows the development view to show the extracted source fragment
  • Loading branch information
Mario Caropreso committed Nov 30, 2023
1 parent 70e8f28 commit de733f3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion activesupport/lib/active_support/syntax_error_proxy.rb
Expand Up @@ -43,7 +43,8 @@ def backtrace_locations

private
def parse_message_for_trace
if __getobj__.to_s.start_with?("(eval")
# If RUBY_VERSION < 3.2, the path method is not available for SyntaxError
if is_eval?
# If the exception is coming from a call to eval, we need to keep
# the path of the file in which eval was called to ensure we can
# return the right source fragment to show the location of the
Expand All @@ -54,5 +55,15 @@ def parse_message_for_trace
__getobj__.to_s.split("\n")
end
end

if SyntaxError.method_defined?(:path) # Ruby 3.3+
def is_eval?
__getobj__.path.start_with?("(eval")
end
else # 3.2 and older versions of Ruby
def is_eval?
__getobj__.to_s.start_with?("(eval")
end
end
end
end

0 comments on commit de733f3

Please sign in to comment.