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.



69
70
71
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 69

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

Instance Attribute Details

#template_bodyObject (readonly)

Returns the value of attribute template_body.



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

def template_body
  @template_body
end

Instance Method Details

#detailed_messageObject

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



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

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.



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

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



84
85
86
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 84

def message
  "Error when processing Jbuilder template"
end

#template_backtrace_locationObject



73
74
75
76
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 73

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.



80
81
82
# File 'lib/jekyll/geolexica/jbuilder_tag.rb', line 80

def template_lineno
  template_backtrace_location.lineno
end