Class: Console::Event::Failure
- Defined in:
- lib/console/event/failure.rb
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #format(output, terminal, verbose) ⇒ Object
- #format_exception(exception, prefix, output, terminal, verbose) ⇒ Object
-
#initialize(exception, root = nil) ⇒ Failure
constructor
A new instance of Failure.
- #to_h ⇒ Object
Methods inherited from Generic
Constructor Details
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
27 28 29 |
# File 'lib/console/event/failure.rb', line 27 def exception @exception end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
28 29 30 |
# File 'lib/console/event/failure.rb', line 28 def root @root end |
Class Method Details
.current_working_directory ⇒ Object
12 13 14 15 16 |
# File 'lib/console/event/failure.rb', line 12 def self.current_working_directory Dir.getwd rescue # e.g. Errno::EMFILE nil end |
.for(exception) ⇒ Object
18 19 20 |
# File 'lib/console/event/failure.rb', line 18 def self.for(exception) self.new(exception, self.current_working_directory) end |
.register(terminal) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/console/event/failure.rb', line 30 def self.register(terminal) terminal[:exception_title] ||= terminal.style(:red, nil, :bold) terminal[:exception_detail] ||= terminal.style(:yellow) terminal[:exception_backtrace] ||= terminal.style(:red) terminal[:exception_backtrace_other] ||= terminal.style(:red, nil, :faint) terminal[:exception_message] ||= terminal.style(:default) end |
Instance Method Details
#format(output, terminal, verbose) ⇒ Object
42 43 44 |
# File 'lib/console/event/failure.rb', line 42 def format(output, terminal, verbose) format_exception(@exception, nil, output, terminal, verbose) end |
#format_exception(exception, prefix, output, terminal, verbose) ⇒ Object
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 |
# File 'lib/console/event/failure.rb', line 46 def format_exception(exception, prefix, output, terminal, verbose) lines = exception..lines.map(&:chomp) output.puts " #{prefix}#{terminal[:exception_title]}#{exception.class}#{terminal.reset}: #{lines.shift}" lines.each do |line| output.puts " #{terminal[:exception_detail]}#{line}#{terminal.reset}" end root_pattern = /^#{@root}\// if @root exception.backtrace&.each_with_index do |line, index| path, offset, = line.split(":") style = :exception_backtrace # Make the path a bit more readable if root_pattern and path.sub!(root_pattern, "").nil? style = :exception_backtrace_other end output.puts " #{index == 0 ? "→" : " "} #{terminal[style]}#{path}:#{offset}#{terminal[:exception_message]} #{}#{terminal.reset}" end if exception.cause format_exception(exception.cause, "Caused by ", output, terminal, verbose) end end |
#to_h ⇒ Object
38 39 40 |
# File 'lib/console/event/failure.rb', line 38 def to_h {exception: @exception, root: @root} end |