Exception: Stellar::Erb::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/stellar/erb.rb,
lib/stellar/erb/error.rb

Overview

:nodoc: This module provides a lightweight ERB template rendering system with error handling capabilities.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, template_path: nil, original_error: nil, line_number: nil) ⇒ Error

Returns a new instance of Error.



8
9
10
11
12
13
14
# File 'lib/stellar/erb/error.rb', line 8

def initialize(message = nil, template_path: nil, original_error: nil, line_number: nil)
  @template_path = template_path
  @original_error = original_error
  @line_number = line_number
  message ||= self.class.name
  super(message)
end

Instance Attribute Details

#line_numberObject

Returns the value of attribute line_number.



6
7
8
# File 'lib/stellar/erb/error.rb', line 6

def line_number
  @line_number
end

#original_errorObject

Returns the value of attribute original_error.



6
7
8
# File 'lib/stellar/erb/error.rb', line 6

def original_error
  @original_error
end

#template_pathObject

Returns the value of attribute template_path.



6
7
8
# File 'lib/stellar/erb/error.rb', line 6

def template_path
  @template_path
end

Class Method Details

.extract_line_number(error, template_path) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/stellar/erb/error.rb', line 39

def self.extract_line_number(error, template_path)
  return nil unless template_path && error.backtrace

  backtrace_line = error.backtrace.find { |line| line.include?(template_path) }
  return nil unless backtrace_line

  match = backtrace_line.match(/:(\d+):/)
  match ? match[1].to_i : nil
end

.wrap(error, template_path: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stellar/erb/error.rb', line 27

def self.wrap(error, template_path: nil)
  return error if error.is_a?(self)

  line_number = extract_line_number(error, template_path)
  message = error.message

  new(message,
      template_path: template_path,
      original_error: error,
      line_number: line_number)
end

Instance Method Details

#context_lines(number_of_lines = 5) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stellar/erb/error.rb', line 49

def context_lines(number_of_lines = 5)
  return [] unless template_path && line_number && File.exist?(template_path)

  lines = File.readlines(template_path)
  start_line = [line_number - number_of_lines - 1, 0].max
  end_line = [line_number + number_of_lines - 1, lines.length - 1].min

  context = []
  (start_line..end_line).each do |i|
    prefix = i + 1 == line_number ? "=>" : "  "
    context << "#{prefix} #{i + 1}: #{lines[i].chomp}"
  end

  context
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/stellar/erb/error.rb', line 16

def to_s
  message = super
  if template_path && line_number
    "#{message} in '#{template_path}' on line #{line_number}"
  elsif template_path
    "#{message} in '#{template_path}'"
  else
    message
  end
end