Sha256: 6afa0d67cdf43e58c77ff269299fc781d4a5df1e50400e965f63e0ce8cdb82cf

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module DslEvaluator
  class Printer
    def initialize(error)
      @error = error
    end

    def message
      @error.message
    end

    # Prints out a user friendly task_definition error message
    def print
      print_source(info)
    end

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

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

    def info_from_backtrace
      lines = @error.backtrace

      backtrace_reject = DslEvaluator.backtrace_reject
      lines = lines.reject { |l| l.include?(backtrace_reject) } if backtrace_reject
      lines = lines.reject { |l| l.include?("lib/dsl_evaluator") } # ignore internal lib/dsl_evaluator backtrace lines

      error_info = lines.first
      path, line_number, _ = error_info.split(':')
      {path: path, line_number: line_number}
    end

    def print_source(info={})
      path = info[:path]
      line_number = info[:line_number].to_i

      puts "Error evaluating #{path}:".color(:red)
      puts @error.message
      puts "Here's the line in #{path} with the error:\n\n"

      contents = IO.read(path)
      content_lines = contents.split("\n")
      context = 5 # lines of context
      top, bottom = [line_number-context-1, 0].max, line_number+context-1
      lpad = content_lines.size.to_s.size
      content_lines[top..bottom].each_with_index do |line_content, index|
        current_line = top+index+1
        if current_line == line_number
          printf("%#{lpad}d %s\n".color(:red), line_number, line_content)
        else
          printf("%#{lpad}d %s\n", line_number, line_content)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dsl_evaluator-0.1.2 lib/dsl_evaluator/printer.rb