Class: DslEvaluator::Printer
- Inherits:
-
Object
- Object
- DslEvaluator::Printer
- Defined in:
- lib/dsl_evaluator/printer.rb,
lib/dsl_evaluator/printer/concern.rb
Defined Under Namespace
Modules: Concern
Instance Method Summary collapse
- #config ⇒ Object
- #error_info ⇒ Object
-
#info_from_backtrace ⇒ Object
Grab info so can print out user friendly error message.
- #info_from_message ⇒ Object
-
#initialize(error) ⇒ Printer
constructor
A new instance of Printer.
- #logger ⇒ Object
- #message ⇒ Object
- #pretty_path(path) ⇒ Object
-
#print ⇒ Object
Prints out a user friendly task_definition error message.
- #print_code(path, line_number) ⇒ Object
- #reject(lines) ⇒ Object
- #select(lines) ⇒ Object
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
#config ⇒ Object
96 97 98 |
# File 'lib/dsl_evaluator/printer.rb', line 96 def config DslEvaluator.config end |
#error_info ⇒ Object
22 23 24 |
# File 'lib/dsl_evaluator/printer.rb', line 22 def error_info @error..include?("syntax") ? : info_from_backtrace end |
#info_from_backtrace ⇒ Object
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..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_message ⇒ Object
26 27 28 29 30 |
# File 'lib/dsl_evaluator/printer.rb', line 26 def error_info = @error. path, line_number, _ = error_info.split(":") {path: path, line_number: line_number} end |
#logger ⇒ Object
92 93 94 |
# File 'lib/dsl_evaluator/printer.rb', line 92 def logger config.logger end |
#message ⇒ Object
100 101 102 |
# File 'lib/dsl_evaluator/printer.rb', line 100 def @error. 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 |
#print ⇒ Object
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.}".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 |
#print_code(path, line_number) ⇒ Object
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 |