Exception: JekyllSupport::CustomError

Inherits:
StandardError show all
Defined in:
lib/error/jekyll_custom_error.rb

Overview

Use like this: CustomError.new(:MyError, ‘blah’, ‘asdf’)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.factory(error_class_name) ⇒ Object



8
9
10
11
12
# File 'lib/error/jekyll_custom_error.rb', line 8

def self.factory(error_class_name)
  return if Object.const_defined? "::#{error_class_name}"

  Object.const_set error_class_name, Class.new(CustomError)
end

Instance Method Details

#calling_fileObject



18
19
20
21
# File 'lib/error/jekyll_custom_error.rb', line 18

def calling_file
  file_fq, _line_number, _extra = backtrace[0]&.split(':')
  file_fq
end

#error_nameObject



14
15
16
# File 'lib/error/jekyll_custom_error.rb', line 14

def error_name
  self.class.name.split('::').last
end

#html_messageObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/error/jekyll_custom_error.rb', line 24

def html_message
  shorten_backtrace
  path, line_number, _caller = backtrace[1]&.split(':')
  <<~END_MSG
    <div class='#{error_name.snakecase}'>
      #{self.class} raised in #{calling_file} while processing line #{line_number} (after front matter) of #{path}
      #{message}
    </div>
  END_MSG
end

#logger_messageObject



35
36
37
38
39
40
41
42
43
# File 'lib/error/jekyll_custom_error.rb', line 35

def logger_message
  shorten_backtrace
  kaller = caller(1..1).first
  path, line_number, _caller = backtrace[1]&.split(':')
  <<~END_MSG
    #{error_name} raised in #{kaller} while processing line #{line_number} (after front matter) of #{path}
      #{message}
  END_MSG
end

#shorten_backtrace(backtrace_element_count = 3) ⇒ Object



45
46
47
48
49
50
# File 'lib/error/jekyll_custom_error.rb', line 45

def shorten_backtrace(backtrace_element_count = 3)
  b = backtrace[0..backtrace_element_count - 1].map do |x|
    x.gsub(Dir.pwd + '/', './')
  end
  set_backtrace b
end