Exception: Webgen::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/webgen/error.rb

Overview

This error class and its descendants are only used in webgen when user-visible errors need to be created. For example, when the format of the configuration is not valid. Use the built-in Ruby error classes for all other error situations!

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg_or_error, location = nil, path = nil) ⇒ Error

Create a new Error object.

If msg_or_error is a String, it is treated as the error message. If it is an exception, the exception is wrapped. The location parameter can be used to further describe where the error happened and the path parameter can be used to associate a source or destination path with the error.



22
23
24
25
26
27
28
29
30
# File 'lib/webgen/error.rb', line 22

def initialize(msg_or_error, location = nil, path = nil)
  if msg_or_error.kind_of?(String)
    super(msg_or_error)
  else
    super(msg_or_error.message)
    set_backtrace(msg_or_error.backtrace)
  end
  @location, @path = location, path.to_s
end

Instance Attribute Details

#locationObject

The location where the error happened (this can be set to a file name, a class name, …).



11
12
13
# File 'lib/webgen/error.rb', line 11

def location
  @location
end

#pathObject

Contains the path name if the error is related to a specific source or destination path,



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

def path
  @path
end

Class Method Details

.error_file(error) ⇒ Object

Return the file name where the error occured.



46
47
48
# File 'lib/webgen/error.rb', line 46

def self.error_file(error)
  (error.is_a?(::SyntaxError) ? error.message : error.backtrace[0]).scan(/(?:^|\s)(.*?):(\d+)/).first.first
end

.error_line(error) ⇒ Object

Return the error line by inspecting the backtrace of the given error instance.



41
42
43
# File 'lib/webgen/error.rb', line 41

def self.error_line(error)
  (error.is_a?(::SyntaxError) ? error.message : error.backtrace[0]).scan(/:(\d+)/).first.first.to_i rescue nil
end

Instance Method Details

#message(wrapped_msg_only = false) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
# File 'lib/webgen/error.rb', line 32

def message(wrapped_msg_only = false) # :nodoc:
  return super().gsub(/\n/, "\n    ") if wrapped_msg_only
  msg = 'Error'
  msg << " at #{@location}" if @location
  msg << (!@path.to_s.empty? ? " while working on <#{@path}>" : '')
  msg << ":\n    " << super().gsub(/\n/, "\n    ")
end