Class: DslEvaluator::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_evaluator/printer.rb,
lib/dsl_evaluator/printer/concern.rb

Defined Under Namespace

Modules: Concern

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Printer

Returns a new instance of Printer.



3
4
5
# File 'lib/dsl_evaluator/printer.rb', line 3

def initialize(error)
  @error = error
end

Instance Method Details

#configObject



96
97
98
# File 'lib/dsl_evaluator/printer.rb', line 96

def config
  DslEvaluator.config
end

#error_infoObject



22
23
24
# File 'lib/dsl_evaluator/printer.rb', line 22

def error_info
  @error.message.include?("syntax") ? info_from_message : info_from_backtrace
end

#info_from_backtraceObject

Grab info so can print out user friendly error message

Backtrace lines are different for OSes:

windows: "C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/terraspace-1.1.1/lib/terraspace/builder.rb:34:in `build'"
linux: "/home/ec2-user/.rvm/gems/ruby-3.0.3/gems/terraspace-1.1.1/lib/terraspace/compiler/dsl/syntax/mod.rb:4:in `<module:Mod>'"


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dsl_evaluator/printer.rb', line 39

def info_from_backtrace
  lines = @error.backtrace
  if ENV["FULL_BACKTRACE"]
    logger.error @error.message.color(:red)
    logger.error lines.join("\n")
  else
    lines = reject(lines)
    lines = select(lines)
  end

  error_info = lines.first
  parts = error_info.split(":")
  windows = error_info.match(/^[a-zA-Z]:/)
  path = windows ? parts[1] : parts[0]
  line_number = windows ? parts[2] : parts[1]
  line_number = line_number.to_i

  {path: path, line_number: line_number}
end

#info_from_messageObject



26
27
28
29
30
# File 'lib/dsl_evaluator/printer.rb', line 26

def info_from_message
  error_info = @error.message
  path, line_number, _ = error_info.split(":")
  {path: path, line_number: line_number}
end

#loggerObject



92
93
94
# File 'lib/dsl_evaluator/printer.rb', line 92

def logger
  config.logger
end

#messageObject



100
101
102
# File 'lib/dsl_evaluator/printer.rb', line 100

def message
  @error.message
end

#pretty_path(path) ⇒ Object



88
89
90
# File 'lib/dsl_evaluator/printer.rb', line 88

def pretty_path(path)
  path.sub("#{config.root}/", "")
end

Prints out a user friendly task_definition error message



8
9
10
11
12
13
14
15
16
# File 'lib/dsl_evaluator/printer.rb', line 8

def print
  info = error_info
  path = info[:path]
  line_number = info[:line_number].to_i
  logger.error "ERROR: #{@error.message}".color(:red)
  logger.error "Error evaluating #{pretty_path(path)}".color(:red)
  logger.error "Here's line #{line_number} with the error:\n\n"
  print_code(path, line_number)
end


18
19
20
# File 'lib/dsl_evaluator/printer.rb', line 18

def print_code(path, line_number)
  DslEvaluator.print_code(path, line_number)
end

#reject(lines) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dsl_evaluator/printer.rb', line 59

def reject(lines)
  # Keep DslEvaluator.backtrace_reject for backwards compatibility
  pattern = config.backtrace.reject_pattern || DslEvaluator.backtrace_reject
  return lines unless pattern

  lines.reject! do |l|
    if pattern.is_a?(String)
      l.include?(pattern)
    else
      l.match(pattern)
    end
  end
  # Always ignore internal lib/dsl_evaluator backtrace lines
  lines.reject { |l| l.include?("lib/dsl_evaluator") }
end

#select(lines) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dsl_evaluator/printer.rb', line 75

def select(lines)
  pattern = config.backtrace.select_pattern
  return lines unless pattern

  lines.select do |l|
    if pattern.is_a?(String)
      l.include?(pattern)
    else
      l.match(pattern)
    end
  end
end