Class: Console::Event::Failure

Inherits:
Generic
  • Object
show all
Defined in:
lib/console/event/failure.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#to_json

Constructor Details

#initialize(exception, root = nil) ⇒ Failure

Returns a new instance of Failure.



22
23
24
25
# File 'lib/console/event/failure.rb', line 22

def initialize(exception, root = nil)
  @exception = exception
  @root = root
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



27
28
29
# File 'lib/console/event/failure.rb', line 27

def exception
  @exception
end

#rootObject (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_directoryObject



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.message.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, message = line.split(":", 3)
    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]} #{message}#{terminal.reset}"
  end
  
  if exception.cause
    format_exception(exception.cause, "Caused by ", output, terminal, verbose)
  end
end

#to_hObject



38
39
40
# File 'lib/console/event/failure.rb', line 38

def to_h
  {exception: @exception, root: @root}
end