Skip to content

Commit e744a6c

Browse files
committed
Ruby CLI: Show compiled source when compile produces invalid Ruby
1 parent aa12f6f commit e744a6c

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/herb/cli.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,24 @@ def compile_template
462462
end
463463

464464
exit(0)
465+
rescue Herb::Engine::InvalidRubyError => e
466+
if json
467+
result = {
468+
success: false,
469+
error: e.message,
470+
source: e.compiled_source,
471+
filename: @file,
472+
}
473+
puts result.to_json
474+
elsif silent
475+
puts "Failed"
476+
else
477+
puts e.compiled_source if e.compiled_source
478+
puts
479+
puts e.message
480+
end
481+
482+
exit(1)
465483
rescue Herb::Engine::CompilationError => e
466484
if json
467485
result = {

lib/herb/engine.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class CompilationError < StandardError
3232
end
3333

3434
class InvalidRubyError < CompilationError
35+
attr_reader :compiled_source
36+
37+
def initialize(message, compiled_source: nil)
38+
@compiled_source = compiled_source
39+
40+
super(message)
41+
end
3542
end
3643

3744
def initialize(input, properties = {})
@@ -155,7 +162,7 @@ def initialize(input, properties = {})
155162

156163
if syntax_errors.any?
157164
details = syntax_errors.map { |e| " - #{e.message} (line #{e.location.start_line})" }.join("\n")
158-
raise InvalidRubyError, "Compiled template produced invalid Ruby:\n#{details}"
165+
raise InvalidRubyError.new("Compiled template produced invalid Ruby:\n#{details}", compiled_source: @src)
159166
end
160167
end
161168

sig/herb/engine.rbs

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)