class Blufin::SqlErrorHandler
Constants
- ERROR_SQL_REGEX_MISMATCH
- ERROR_SQL_SCHEMA_MISMATCH
- ERROR_SQL_STATEMENT_NOT_EXPECTED
Public Class Methods
new(site)
click to toggle source
Initializes the Error Handler. @return void
# 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
Public Instance Methods
add_error(type, file, line, multi_line_content = nil)
click to toggle source
Adds an error to the @error Array. @return void
# 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)
click to toggle source
Displays errors (if any) and halts the script if so. @return Boolean
# 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_meta()
click to toggle source
Returns a Hash of valid error types with titles/descriptions. Moved to top of file for easier programming. @return Hash
# 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
Private Instance Methods
split_to_path_file(path_and_file)
click to toggle source
Removes the majority of the pathname to file. @return String
# File lib/core/error_handling/sql_error_handler.rb, line 133 def split_to_path_file(path_and_file) path = '' file = '' file_parts = path_and_file.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 [path, file] end
validate_error_type(type)
click to toggle source
Validate the error type. @return String
# File lib/core/error_handling/sql_error_handler.rb, line 126 def validate_error_type(type) raise RuntimeError, "Invalid error type: #{type} \xe2\x80\x94 perhaps you forgot to add it to the valid type array in Blufin::SqlErrorHandler ?" unless error_type_meta.keys.include?(type) type end
validate_file(file)
click to toggle source
Validate the file. @return String
# File lib/core/error_handling/sql_error_handler.rb, line 111 def validate_file(file) raise RuntimeError, "File does not exist: #{Blufin::Terminal::format_flag(file)}" unless Blufin::Files::file_exists(file) file end
validate_line(line)
click to toggle source
Validate the line number. @return String
# File lib/core/error_handling/sql_error_handler.rb, line 118 def validate_line(line) line = line.to_s.strip raise RuntimeError, "Expected integer, instead got: #{line}" unless line =~ /^\d+$/ line end