Class: Blufin::ScannerError

Inherits:
Object
  • Object
show all
Extended by:
Columnist
Defined in:
lib/scan/scanner_error.rb

Constant Summary collapse

HEADER_COLOR =
240
LINE_COLOR =
184
ERROR_COLOR =
196
CODE_COLOR =
102

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, line, line_number, message) ⇒ Object

Initialize a ScannerError object.

Raises:

  • (RuntimeError)


16
17
18
19
20
21
22
23
24
# File 'lib/scan/scanner_error.rb', line 16

def initialize(file, line, line_number, message)
    raise RuntimeError, "File does not exist: #{file}" unless Blufin::Files::file_exists(file) unless file.nil?
    raise RuntimeError, "Line number must be positive integer, got: #{line_number}" if !line_number.nil? && line_number.to_s !~ /^\d+$/
    raise RuntimeError, 'Message cannot be nil or blank.' if message.nil? || message.strip == ''
    self.file        = file.nil? ? "\xe2\x80\x94" : file
    self.line        = line.nil? ? "\xe2\x80\x94" : line
    self.line_number = line_number.nil? ? "\xe2\x80\x94" : line_number.to_s
    self.message     = message
end

Instance Attribute Details

#error_typeObject

Returns the value of attribute error_type.



5
6
7
# File 'lib/scan/scanner_error.rb', line 5

def error_type
  @error_type
end

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/scan/scanner_error.rb', line 5

def file
  @file
end

#lineObject

Returns the value of attribute line.



5
6
7
# File 'lib/scan/scanner_error.rb', line 5

def line
  @line
end

#line_numberObject

Returns the value of attribute line_number.



5
6
7
# File 'lib/scan/scanner_error.rb', line 5

def line_number
  @line_number
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/scan/scanner_error.rb', line 5

def message
  @message
end

Class Method Details

.add_error(file, message, line = nil, line_number = nil) ⇒ Object

Add an error.

Returns:

  • Object



78
79
80
# File 'lib/scan/scanner_error.rb', line 78

def self.add_error(file, message, line = nil, line_number = nil)
    Blufin::ScannerError.new(file, line, line_number, message)
end

.highlight_filename(file) ⇒ Object

Takes a file like ‘/Users/Albert/Repos/eworldes/fmm/client/src/js/app/overlays.js’ and highlights ‘overlays.js’

Returns:

  • string

Raises:

  • (RuntimeError)


69
70
71
72
73
74
# File 'lib/scan/scanner_error.rb', line 69

def self.highlight_filename(file)
    raise RuntimeError, "Expected String, instead got: #{file.class}" unless file.is_a?(String)
    fs = file.split('/')
    fl = fs.pop
    "\x1B[48;5;233m \x1B[38;5;240m#{fs.join('/')}/\x1B[38;5;202m#{fl} \x1B[0m"
end

.output_cli(errors, clear_screen = true) ⇒ Object

Static method to output the errors to Terminal.

Returns:

  • void

Raises:

  • (RuntimeError)


28
29
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
# File 'lib/scan/scanner_error.rb', line 28

def self.output_cli(errors, clear_screen = true)
    raise RuntimeError, "Expected Array, instead got: #{errors.class}" unless errors.is_a?(Array)
    return unless errors.any?
    system('clear') if clear_screen
    puts if clear_screen
    @files = {}
    errors.each do |error|
        file         = error.file
        @files[file] = [] unless @files.has_key?(file)
        @files[file] << error
    end
    @files.each do |file, inner_errors|
        puts "   #{highlight_filename(file)}"
        table(:border => false) do
            wildcard_width = Blufin::Terminal::get_terminal_width - 120
            wildcard_width = 1 if wildcard_width < 0
            message_width  = 100
            row do
                column('', :width => 2, :color => HEADER_COLOR)
                column('-' * 5, :width => 5, :color => HEADER_COLOR)
                column('-' * message_width, :width => message_width, :color => HEADER_COLOR)
                column('-' * wildcard_width, :width => wildcard_width, :color => HEADER_COLOR)
                column('', :width => 2, :color => HEADER_COLOR)
            end
            inner_errors.each do |error|
                row do
                    column('')
                    column(error.line_number[0..5].ljust(5, ' '), :color => LINE_COLOR)
                    column(error.message[0..message_width], :color => ERROR_COLOR)
                    column(error.line.strip[0..wildcard_width], :color => CODE_COLOR)
                    column('')
                end
            end
        end
        puts
    end
    exit 1
end