Class: Console::Event::Failure

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

Overview

Represents a failure of some kind, usually with an attached exception.

“‘ruby begin raise “Something went wrong!” rescue => exception Console::Event::Failure.log(“Something went wrong!”, exception) end “`

Generally, you should use the Console.error method to log failures, as it will automatically create a failure event for you.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#as_json, #to_json, #to_s

Constructor Details

#initialize(exception, root = self.class.default_root) ⇒ Failure

Create a new failure event for the given exception.



56
57
58
59
# File 'lib/console/event/failure.rb', line 56

def initialize(exception, root = self.class.default_root)
	@exception = exception
	@root = root
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



50
51
52
# File 'lib/console/event/failure.rb', line 50

def exception
  @exception
end

Class Method Details

.default_rootObject

For the purpose of efficiently formatting backtraces, we need to know the root directory of the project.



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

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

.for(exception) ⇒ Object

Create a new failure event for the given exception.



36
37
38
# File 'lib/console/event/failure.rb', line 36

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

.log(subject, exception, **options) ⇒ Object

Log a failure event with the given exception.



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

def self.log(subject, exception, **options)
	Console.error(subject, **self.for(exception).to_hash, **options)
end

Instance Method Details

#emit(*arguments, **options) ⇒ Object

Log the failure event.



76
77
78
79
80
# File 'lib/console/event/failure.rb', line 76

def emit(*arguments, **options)
	options[:severity] ||= :error
	
	super
end

#The exception which caused the failure.=(exceptionwhichcausedthefailure. = (value)) ⇒ Object



50
# File 'lib/console/event/failure.rb', line 50

attr_reader :exception

#to_hashObject

Convert the failure event to a hash.



64
65
66
67
68
69
70
# File 'lib/console/event/failure.rb', line 64

def to_hash
	Hash.new.tap do |hash|
		hash[:type] = :failure
		hash[:root] = @root if @root
		extract(@exception, hash)
	end
end