Module: JekyllSupportError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



2
3
4
# File 'lib/error/jekyll_plugin_error_handling.rb', line 2

def logger
  @logger
end

#pageObject (readonly)

Returns the value of attribute page.



2
3
4
# File 'lib/error/jekyll_plugin_error_handling.rb', line 2

def page
  @page
end

Instance Method Details

#exit_without_stack_trace(error, caller_index = 1) ⇒ Object

If a Jekyll plugin needs to crash exit, and stop Jekyll, call this method. It does not generate a stack trace. This method does not return because the process is abruptly terminated.

Do not raise the error before calling this method, just create it via ‘new’, like this: exit_without_stack_trace StandardError.new(‘This is my error message’)

If you want to call this method from a handler method, the default index for the backtrace array must be specified. The default backtrace index is 1, which means the calling method. To specify the calling method’s caller, pass in 2, like this: exit_without_stack_trace StandardError.new(‘This is my error message’), 2

Parameters:

  • error

    StandardError or a subclass of StandardError is required



17
18
19
20
21
22
23
24
25
# File 'lib/error/jekyll_plugin_error_handling.rb', line 17

def exit_without_stack_trace(error, caller_index = 1)
  raise error
rescue StandardError => e
  file, line_number, caller = e.backtrace[caller_index].split(':')
  caller = caller.tr('`', "'")
  warn "#{error.message} #{caller} on line #{line_number} (after front matter) of #{file}".red
  # Process.kill('HUP', Process.pid) # generates huge stack trace
  exec "echo ''"
end

#format_error_message(message) ⇒ Object



27
28
29
30
# File 'lib/error/jekyll_plugin_error_handling.rb', line 27

def format_error_message(message)
  page = " of #{@page['path']}" if @page
  remove_ansi_color "on line #{line_number} (after front matter)#{page}.\n#{message}"
end

#maybe_reraise_error(error, throw_error: true) ⇒ Object



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

def maybe_reraise_error(error, throw_error: true)
  fmsg = format_error_message "#{error.class}: #{error.message.strip}"
  @logger.error { fmsg }
  return "<span class='jekyll_plugin_support_error'>#{fmsg}</span>" unless throw_error

  error.set_backtrace error.backtrace[0..9]
  raise error
end

#remove_ansi_color(string) ⇒ Object



32
33
34
# File 'lib/error/jekyll_plugin_error_handling.rb', line 32

def remove_ansi_color(string)
  string.gsub(/\e\[([;\d]+)?m/, '')
end

#warn_short_trace(error) ⇒ Object



45
46
47
# File 'lib/error/jekyll_plugin_error_handling.rb', line 45

def warn_short_trace(error)
  JekyllSupport.warn_short_trace(@logger, error)
end