summaryrefslogtreecommitdiff
path: root/lib/chef/mixin
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2019-05-17 05:31:51 +0200
committerGabriel Mazetto <brodock@gmail.com>2019-05-26 14:47:06 +0200
commitbf23769d7901a79623732d0db68d11a832b3ab40 (patch)
tree16715b2d80c6803a8f26921bf0731b69ce556763 /lib/chef/mixin
parentdf885a1e35366e6180632741918ab0e774c671a4 (diff)
downloadchef-bf23769d7901a79623732d0db68d11a832b3ab40.tar.gz
Improving error handling for template render
While Chef already implements a nice amount of information in `TemplateError#to_s`, when using ChefSpec with RSpec, the `#to_s` is not called, leaving us with no source information for template errors. By adding the filename to `Erubis::Eruby` initialization, we can get a better backtrace that will show up in RSpec output. Signed-off-by: Gabriel Mazetto <brodock@gmail.com>
Diffstat (limited to 'lib/chef/mixin')
-rw-r--r--lib/chef/mixin/template.rb23
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb
index bb811aa758..4ec559fab1 100644
--- a/lib/chef/mixin/template.rb
+++ b/lib/chef/mixin/template.rb
@@ -138,11 +138,11 @@ class Chef
partial_context._extend_modules(@_extension_modules)
template_location = @template_finder.find(partial_name, options)
- _render_template(IO.binread(template_location), partial_context)
+ _render_template(IO.binread(template_location), partial_context, filename: template_location)
end
def render_template(template_location)
- _render_template(IO.binread(template_location), self)
+ _render_template(IO.binread(template_location), self, filename: template_location)
end
def render_template_from_string(template)
@@ -153,12 +153,13 @@ class Chef
# INTERNAL PUBLIC API
###
- def _render_template(template, context)
+ def _render_template(template, context, options = {})
begin
- eruby = Erubis::Eruby.new(template)
+ # eruby = Erubis::Eruby.new(template, options)
+ eruby = Erubis::Eruby.new(template, options)
output = eruby.evaluate(context)
rescue Object => e
- raise TemplateError.new(e, template, context)
+ raise TemplateError.new(e, template, context, options)
end
# CHEF-4399
@@ -210,11 +211,11 @@ class Chef
end
class TemplateError < RuntimeError
- attr_reader :original_exception, :context
+ attr_reader :original_exception, :context, :options
SOURCE_CONTEXT_WINDOW = 2
- def initialize(original_exception, template, context)
- @original_exception, @template, @context = original_exception, template, context
+ def initialize(original_exception, template, context, options)
+ @original_exception, @template, @context, @options = original_exception, template, context, options
end
def message
@@ -222,7 +223,11 @@ class Chef
end
def line_number
- @line_number ||= $1.to_i if original_exception.backtrace.find { |line| line =~ /\(erubis\):(\d+)/ }
+ @line_number ||= if options[:filename]
+ $1.to_i if original_exception.backtrace.find { |line| line =~ /#{Regexp.escape(options[:filename])}:(\d+)/ }
+ else
+ $1.to_i if original_exception.backtrace.find { |line| line =~ /\(erubis\):(\d+)/ }
+ end
end
def source_location