Skip to content

Commit 60d729f

Browse files
committed
Embeded ruby has consistent exception handling
Ruby code that can raise exceptions is invoked within a single `rescue` block within ERBRenderer (see: template_evaluation_context.rb). - JSON reading, parsing moves into ruby `ERBRenderer` - `TemplateEvaluationContext` initiization moves into `ERBRenderer` - invocation of `TemplateEvaluationContext#name` uses nil-safe acccessor
1 parent 38e1362 commit 60d729f

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

templatescompiler/erbrenderer/erb_renderer_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ property3: default_value3
118118
})
119119

120120
Describe("error handling within Ruby", func() {
121-
const rubyExceptionPrefix = "Error filling in template " // see template_evaluation_context.rb
121+
var (
122+
// see template_evaluation_context.rb
123+
rubyExceptionPrefixTemplate = "Error filling in template '%s' "
124+
)
122125

123126
Context("with invalid ERB", func() {
124127
BeforeEach(func() {
@@ -132,16 +135,25 @@ property3: default_value3
132135
err := erbRenderer.Render(erbTemplateFilepath, renderedTemplatePath, context)
133136
Expect(err).To(HaveOccurred())
134137
Expect(err.Error()).To(ContainSubstring("RuntimeError: test error"))
135-
Expect(err.Error()).To(ContainSubstring(rubyExceptionPrefix))
138+
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(rubyExceptionPrefixTemplate, erbTemplateFilepath)))
136139
})
137140

138141
Context("with missing ERB template", func() {
139142
It("returns an error with a known ruby exception", func() {
140143
invalidErbPath := "invalid/template.erb"
141144
err := erbRenderer.Render(invalidErbPath, renderedTemplatePath, context)
142145
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))
146+
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("<Errno::ENOENT: No such file or directory @ rb_sysopen - %s>", invalidErbPath)))
147+
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(rubyExceptionPrefixTemplate, invalidErbPath)))
148+
})
149+
})
150+
151+
Context("with context JSON which does not have the expected elements", func() {
152+
It("returns an error with a known ruby exception", func() {
153+
err := erbRenderer.Render(erbTemplateFilepath, renderedTemplatePath, &testTemplateEvaluationContext{})
154+
Expect(err).To(HaveOccurred())
155+
Expect(err.Error()).To(ContainSubstring("undefined method `recursive_merge!'"))
156+
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(rubyExceptionPrefixTemplate, erbTemplateFilepath)))
145157
})
146158
})
147159
})

templatescompiler/erbrenderer/template_evaluation_context.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,23 @@ def dump(*args)
166166
end
167167

168168
class ERBRenderer
169-
def initialize(context)
170-
@context = context
169+
def initialize(json_context_path)
170+
@json_context_path = json_context_path
171171
end
172172

173173
def render(src_path, dst_path)
174174
erb = ERB.new(File.read(src_path), safe_level = nil, trim_mode = "-")
175175
erb.filename = src_path
176176

177+
context_hash = JSON.load(File.read(@json_context_path))
178+
template_evaluation_context = TemplateEvaluationContext.new(context_hash)
179+
177180
File.open(dst_path, "w") do |f|
178-
f.write(erb.result(@context.get_binding))
181+
f.write(erb.result(template_evaluation_context.get_binding))
179182
end
180183

181184
rescue Exception => e
182-
name = "#{@context.name}/#{@context.index}"
185+
name = "#{template_evaluation_context&.name}/#{template_evaluation_context&.index}"
183186

184187
line_i = e.backtrace.index { |l| l.include?("#{erb&.filename}") }
185188
line_num = line_i ? e.backtrace[line_i].split(':')[1] : "unknown"
@@ -192,9 +195,6 @@ def render(src_path, dst_path)
192195
if $0 == __FILE__
193196
json_context_path, erb_template_path, rendered_template_path = *ARGV
194197

195-
context_hash = JSON.load(File.read(json_context_path))
196-
context = TemplateEvaluationContext.new(context_hash)
197-
198-
renderer = ERBRenderer.new(context)
198+
renderer = ERBRenderer.new(json_context_path)
199199
renderer.render(erb_template_path, rendered_template_path)
200200
end

0 commit comments

Comments
 (0)