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

Constructor Details

#initialize(exception, root = nil) ⇒ Failure

Returns a new instance of Failure.



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

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

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



43
44
45
# File 'lib/console/event/failure.rb', line 43

def exception
  @exception
end

#rootObject (readonly)

Returns the value of attribute root.



44
45
46
# File 'lib/console/event/failure.rb', line 44

def root
  @root
end

Class Method Details

.current_working_directoryObject



28
29
30
31
32
# File 'lib/console/event/failure.rb', line 28

def self.current_working_directory
	Dir.getwd
rescue # e.g. Errno::EMFILE
	nil
end

.for(exception) ⇒ Object



34
35
36
# File 'lib/console/event/failure.rb', line 34

def self.for(exception)
	self.new(exception, self.current_working_directory)
end

.register(terminal) ⇒ Object



46
47
48
49
50
# File 'lib/console/event/failure.rb', line 46

def self.register(terminal)
	terminal[:exception_title] ||= terminal.style(:red, nil, :bold)
	terminal[:exception_detail] ||= terminal.style(:yellow)
	terminal[:exception_backtrace] ||= terminal.style(:red)
end

Instance Method Details

#format(output, terminal, verbose) ⇒ Object



52
53
54
# File 'lib/console/event/failure.rb', line 52

def format(output, terminal, verbose)
	format_exception(@exception, nil, output, terminal, verbose)
end

#format_exception(exception, prefix, output, terminal, verbose) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/console/event/failure.rb', line 56

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
	
	exception.backtrace&.each_with_index do |line, index|
		path, offset, message = line.split(":")
		
		# Make the path a bit more readable
		path.gsub!(/^#{@root}\//, "./") if @root
		
		output.puts "  #{index == 0 ? "" : " "} #{terminal[:exception_backtrace]}#{path}:#{offset}#{terminal.reset} #{message}"
	end
	
	if exception.cause
		format_exception(exception.cause, "Caused by ", output, terminal, verbose)
	end
end