Class: Blufin::SqlErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/core/error_handling/sql_error_handler.rb

Constant Summary collapse

ERROR_SQL_REGEX_MISMATCH =
100
ERROR_SQL_SCHEMA_MISMATCH =
101
ERROR_SQL_STATEMENT_NOT_EXPECTED =
102

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Object

Initializes the Error Handler.


22
23
24
25
26
27
28
29
# File 'lib/core/error_handling/sql_error_handler.rb', line 22

def initialize(site)

    @site          = Blufin::SiteResolver::validate_site(site)
    @site_sql_path = Blufin::SiteResolver::path_to_sql(@site)

    @errors = []

end

Instance Method Details

#add_error(type, file, line, multi_line_content = nil) ⇒ Object

Adds an error to the @error Array.

Returns:

  • void

Raises:

  • (RuntimeError)

33
34
35
36
# File 'lib/core/error_handling/sql_error_handler.rb', line 33

def add_error(type, file, line, multi_line_content = nil)
    raise RuntimeError, "Expected multi_line_content to be Array, instead got: #{multi_line_content.class}" unless multi_line_content.nil? || multi_line_content.is_a?(Array)
    @errors << Blufin::SqlError.new(validate_error_type(type), validate_file(file), validate_line(line), multi_line_content)
end

#display_errors_if_any(exit = false) ⇒ Object

Displays errors (if any) and halts the script if so.

Returns:

  • Boolean


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/core/error_handling/sql_error_handler.rb', line 40

def display_errors_if_any(exit = false)

    return false unless @errors.any?

    puts

    errors_types          = []
    errors_sorted_by_type = {}
    errors_meta           = error_type_meta
    errors_output         = []

    column_path = []
    column_file = []
    column_line = []

    @errors.each do |error|
        path = ''
        file = ''
        if errors_sorted_by_type[error.type].nil?
            errors_types << error.type
            errors_sorted_by_type[error.type] = []
        end
        errors_sorted_by_type[error.type] << error
        file_parts = error.file.dup.strip.gsub(@site_sql_path, '').gsub(/\A\//, '').split('/')
        file_parts.each_with_index do |part, idx|
            if idx < file_parts.length - 1
                path << "#{part}/"
            else
                file << part
            end
        end
        column_path << path
        column_file << file
        column_line << error.line
    end

    column_path_max = column_path.max_by(&:length).length
    column_file_max = column_file.max_by(&:length).length
    column_line_max = column_line.max_by(&:length).length

    multi_line_padding = (column_path_max + column_file_max + column_line_max + 6).times.collect { ' ' }.join('')

    errors_types.sort!
    errors_types.each do |error_type|
        errors_output << "#{''.ljust(column_path_max - 5, ' ')}\x1B[38;5;231m\x1B[48;5;238m ERROR \x1B[48;5;196m #{errors_meta[error_type][0]} \x1B[0m\x1B[38;5;240m \xe2\x86\x92 \x1B[0m\x1B[38;5;221m#{errors_meta[error_type][1]}\x1B[0m"
        errors_output << ''
        errors_sorted_by_type[error_type].each do |error|
            path_and_file = split_to_path_file(error.file)
            content       = Blufin::Files::read_file(error.file, error.line.dup.to_i, error.line.dup.to_i)
            errors_output << "  \x1B[38;5;240m#{path_and_file[0].rjust(column_path_max, ' ')}\x1B[38;5;196m#{path_and_file[1].ljust(column_file_max, ' ')}\x1B[0m:\x1B[38;5;76m#{error.line.ljust(column_line_max, ' ')}\x1B[0m \x1B[38;5;246m\xe2\x80\x94 \x1B[0m\x1B[48;5;240m#{content[0].gsub("\n", '')}\x1B[0m"
            if error.multi_line_content.is_a?(Array)
                error.multi_line_content.each do |line|
                    errors_output << "#{multi_line_padding}#{line}"
                end
            end
        end
        errors_output << ''
    end

    errors_output.each { |line| puts line }

    exit 1 if exit

    true

end

#error_type_metaObject

Returns a Hash of valid error types with titles/descriptions. Moved to top of file for easier programming.

Returns:

  • Hash


12
13
14
15
16
17
18
# File 'lib/core/error_handling/sql_error_handler.rb', line 12

def error_type_meta
    {
        ERROR_SQL_REGEX_MISMATCH         => ['SQL DATA - REGEX MISMATCH', "Line does not match expected pattern \xe2\x80\x94 please check your formatting."],
        ERROR_SQL_SCHEMA_MISMATCH        => ['SQL DATA - SCHEMA MISMATCH', "Field(s) within a SQL INSERT statement don't match the actual schema."],
        ERROR_SQL_STATEMENT_NOT_EXPECTED => ['SQL DATA - STATEMENT NOT EXPECTED', 'Statement matches REGEX but was not expected in this part of the file.']
    }
end