Skip to content

Commit 28127e2

Browse files
committed
Ruby TemplateEvaluationContext handles missing ERB
This change updates the exception catching in the embeded ruby script (template_evaluation_context.rb) so that when the supplied ERB template argument is not a file on disk the exception handling code does not itself error. Prior to this change if the ruby code attempted to load a non-existent ERB template the exception handling code would fail tyring ot access the ERB instance's `#filename` method.
1 parent 6ef17f3 commit 28127e2

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

templatescompiler/erbrenderer/erb_renderer_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ property3: default_value3
134134
Expect(err.Error()).To(ContainSubstring("RuntimeError: test error"))
135135
Expect(err.Error()).To(ContainSubstring(rubyExceptionPrefix))
136136
})
137+
138+
Context("with missing ERB template", func() {
139+
It("returns an error with a known ruby exception", func() {
140+
invalidErbPath := "invalid/template.erb"
141+
err := erbRenderer.Render(invalidErbPath, renderedTemplatePath, context)
142+
Expect(err).To(HaveOccurred())
143+
Expect(err.Error()).To(MatchRegexp(fmt.Sprintf("No such file or directory .* %s", invalidErbPath)))
144+
Expect(err.Error()).To(ContainSubstring(rubyExceptionPrefix))
145+
})
146+
})
137147
})
138148
})
139149
})

templatescompiler/erbrenderer/template_evaluation_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def render(src_path, dst_path)
181181
rescue Exception => e
182182
name = "#{@context.name}/#{@context.index}"
183183

184-
line_i = e.backtrace.index { |l| l.include?(erb.filename) }
184+
line_i = e.backtrace.index { |l| l.include?("#{erb&.filename}") }
185185
line_num = line_i ? e.backtrace[line_i].split(':')[1] : "unknown"
186186
location = "(line #{line_num}: #{e.inspect})"
187187

0 commit comments

Comments
 (0)