Exception: Stellar::Erb::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Stellar::Erb::Error
- 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
-
#line_number ⇒ Object
Returns the value of attribute line_number.
-
#original_error ⇒ Object
Returns the value of attribute original_error.
-
#template_path ⇒ Object
Returns the value of attribute template_path.
Class Method Summary collapse
Instance Method Summary collapse
- #context_lines(number_of_lines = 5) ⇒ Object
-
#initialize(message = nil, template_path: nil, original_error: nil, line_number: nil) ⇒ Error
constructor
A new instance of Error.
- #to_s ⇒ Object
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( = nil, template_path: nil, original_error: nil, line_number: nil) @template_path = template_path @original_error = original_error @line_number = line_number ||= self.class.name super() end |
Instance Attribute Details
#line_number ⇒ Object
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_error ⇒ Object
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_path ⇒ Object
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) = error. new(, 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_s ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/stellar/erb/error.rb', line 16 def to_s = super if template_path && line_number "#{} in '#{template_path}' on line #{line_number}" elsif template_path "#{} in '#{template_path}'" else end end |