From 16d1351a9309aa909c23c12ec3bb5b777ace9a22 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Tue, 16 Jan 2024 01:05:53 +0000 Subject: [PATCH] Handle nil backtrace_locations in SyntaxErrorProxy --- actionpack/test/dispatch/exception_wrapper_test.rb | 8 ++++++++ activesupport/lib/active_support/syntax_error_proxy.rb | 2 ++ 2 files changed, 10 insertions(+) diff --git a/actionpack/test/dispatch/exception_wrapper_test.rb b/actionpack/test/dispatch/exception_wrapper_test.rb index 33d30d5f957..63efc795b08 100644 --- a/actionpack/test/dispatch/exception_wrapper_test.rb +++ b/actionpack/test/dispatch/exception_wrapper_test.rb @@ -83,6 +83,14 @@ module ActionDispatch end end + test "#source_extracts works with nil backtrace_locations" do + exception = begin eval "class Foo; yield; end"; rescue SyntaxError => ex; ex; end + + wrapper = ExceptionWrapper.new(nil, exception) + + assert_empty wrapper.source_extracts + end + if defined?(ErrorHighlight) && Gem::Version.new(ErrorHighlight::VERSION) >= Gem::Version.new("0.4.0") test "#source_extracts works with error_highlight" do lineno = __LINE__ diff --git a/activesupport/lib/active_support/syntax_error_proxy.rb b/activesupport/lib/active_support/syntax_error_proxy.rb index 87eaca4376f..08baf2f0279 100644 --- a/activesupport/lib/active_support/syntax_error_proxy.rb +++ b/activesupport/lib/active_support/syntax_error_proxy.rb @@ -32,6 +32,8 @@ module ActiveSupport end def backtrace_locations + return nil if super.nil? + parse_message_for_trace.map { |trace| file, line = trace.match(/^(.+?):(\d+).*$/, &:captures) || trace BacktraceLocation.new(file, line.to_i, trace)