Exception: Jekyll::Geolexica::JbuilderTag::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/jekyll/geolexica/jbuilder_tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_body) ⇒ Error

Returns a new instance of Error.



67
68
69
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 67

def initialize(template_body)
  @template_body = template_body.dup
end

Instance Attribute Details

#template_bodyObject (readonly)

Returns the value of attribute template_body.



65
66
67
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 65

def template_body
  @template_body
end

Instance Method Details

#detailed_messageObject

Works only if error has been raised already (requires #cause to be set).



88
89
90
91
92
93
94
95
96
97
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 88

def detailed_message
  <<~MSG
    #{message}:

    #{location_surroundings}

    Caused by:
    #{cause.message}
  MSG
end

#location_surroundings(before: 2, after: 2) ⇒ Object

Displays line that the error has occurred at.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 100

def location_surroundings(before: 2, after: 2)
  line_idx = template_lineno - 1 # template_lineno is indexed from 1

  before_lines = template_body.lines[line_idx - before, before]
  after_lines  = template_body.lines[line_idx + 1, after]
  that_line    = template_body.lines[line_idx]

  before_lines.each { |l| l.prepend("\s" * 4) }
  after_lines.each { |l| l.prepend("\s" * 4) }
  that_line.prepend("==> ")

  [*before_lines, that_line, *after_lines].join("")
end

#messageObject



82
83
84
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 82

def message
  "Error when processing Jbuilder template"
end

#template_backtrace_locationObject



71
72
73
74
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 71

def template_backtrace_location
  @template_backtrace_location ||=
    cause.backtrace_locations.detect { |bl| bl.path == "(eval)" }
end

#template_linenoObject

This is tricky! The line number relates to the tag content, not to the Liquid template.



78
79
80
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 78

def template_lineno
  template_backtrace_location.lineno
end