class Guard::Bosh::TemplateRenderer
Render a template with the provided context and report any errors.
Constants
- ERB_LINE_PATTERN
Public Instance Methods
render(context:, template:)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 9 def render(context:, template:) renderer = ::Bosh::Template::Renderer.new( context: JSON.generate(context)) begin renderer.render(template) { template: template, status: :success, detail: '' } rescue StandardError, SyntaxError => e generate_user_facing_error(template, e) end end
Private Instance Methods
error(template, message, line)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 35 def error(template, message, line) { template: template, status: :failure, detail: message, line: line } end
find_erb_error(error_lines)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 55 def find_erb_error(error_lines) # '(erb):4: syntax error, unexpected keyword_do_block' Array(error_lines).lazy.grep(ERB_LINE_PATTERN) do |error_line| ERB_LINE_PATTERN.match(error_line) end.first end
generate_user_facing_error(template, ex)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 23 def generate_user_facing_error(template, ex) case ex when ::Bosh::Template::UnknownProperty error(template, "missing property: #{ex.name}", line(ex)) when NoMethodError, NameError error(template, remove_bosh_template(ex.message), line(ex)) when SyntaxError context = find_erb_error(ex.message) error(template, context['message'], context['line'].to_i) end end
line(error)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 44 def line(error) erb_error = find_erb_error(error.backtrace) if erb_error.nil? :unknown else erb_error['line'].to_i end end
remove_bosh_template(message)
click to toggle source
# File lib/guard/bosh/template_renderer.rb, line 62 def remove_bosh_template(message) # ' for #<Bosh::Template::EvaluationContext:0x00000000000000>' message.sub(/ for #<Bosh::Template::[^>]+>$/, '') end