Class: Herb::Engine::ErrorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/herb/engine/error_formatter.rb

Constant Summary collapse

CONTEXT_LINES =
3

Instance Method Summary collapse

Constructor Details

#initialize(source, errors, options = {}) ⇒ ErrorFormatter

Returns a new instance of ErrorFormatter.



11
12
13
14
15
16
17
18
# File 'lib/herb/engine/error_formatter.rb', line 11

def initialize(source, errors, options = {})
  @source = source
  @errors = errors
  @filename = options[:filename] || "[source]"
  @lines = source.lines
  @use_highlighter = options.fetch(:use_highlighter, true)
  @highlighter_path = options[:highlighter_path] || find_highlighter_path
end

Instance Method Details

#format_allObject



20
21
22
23
24
25
26
27
28
# File 'lib/herb/engine/error_formatter.rb', line 20

def format_all
  return "No errors found" if @errors.empty?

  if @use_highlighter && @highlighter_path && can_use_highlighter?
    format_all_with_highlighter
  else
    format_all_without_highlighter
  end
end

#format_all_with_highlighterObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/herb/engine/error_formatter.rb', line 30

def format_all_with_highlighter
  output = String.new
  output << "HTML+ERB Compilation Errors:\n"
  output << ("=" * 60) << "\n\n"

  require "tempfile"
  temp_file = Tempfile.new(["herb_error", ".html.erb"])
  temp_file.write(@source)
  temp_file.close

  begin
    highlighted_output = run_highlighter_with_diagnostics(temp_file.path, CONTEXT_LINES)

    if highlighted_output
      output << highlighted_output
    else
       = @errors.group_by do |error|
        location = error.is_a?(Hash) ? error[:location] : error.location
        location&.start&.line
      end.compact

      .each_with_index do |(line_num, line_errors), group_index|
        output << "Error Group ##{group_index + 1} (Line #{line_num}):\n"
        output << ("-" * 40) << "\n"

        line_errors.each_with_index do |error, index|
          output << format_error_header(error, index + 1)
        end

        output << "\nSource Context:\n"

        highlighted_basic = run_highlighter(temp_file.path, line_num, CONTEXT_LINES)

        output << (highlighted_basic || format_source_context_basic(line_errors.first))

        output << "\n"
        output << format_suggestions(line_errors)
        output << "\n" unless group_index == .length - 1
      end
    end

    output << "\n" << ("=" * 60) << "\n"
    output << "Total errors: #{@errors.length}\n"
    output << "Compilation failed. Please fix the errors above.\n"
  ensure
    temp_file.unlink
  end

  output
end

#format_all_without_highlighterObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/herb/engine/error_formatter.rb', line 81

def format_all_without_highlighter
  output = String.new
  output << "HTML+ERB Compilation Errors:\n"
  output << ("=" * 60) << "\n\n"

  @errors.each_with_index do |error, index|
    output << format_error(error, index + 1)
    output << "\n" unless index == @errors.length - 1
  end

  output << "\n" << ("=" * 60) << "\n"
  output << "Total errors: #{@errors.length}\n"
  output << "Compilation failed. Please fix the errors above.\n"

  output
end

#format_error(error, number) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/herb/engine/error_formatter.rb', line 98

def format_error(error, number)
  output = String.new

  error_name = if error.is_a?(Hash)
                 error[:code] || "UnknownError"
               else
                 error.class.name.split("::").last.gsub(/Error$/, "")
               end

  output << "Error ##{number}: #{error_name}\n"
  output << ("-" * 40) << "\n"

  location = error.is_a?(Hash) ? error[:location] : error.location
  if location
    output << "  File: #{@filename}\n"
    output << "  Location: Line #{location.start.line}, Column #{location.start.column}\n"
  end

  error_message = error.is_a?(Hash) ? error[:message] : error.message
  output << "  Message: #{error_message}\n\n"
  output << format_source_context(error) if location
  output << format_error_details(error)

  output
end