ConsoleSourceConsoleEventFailure

class Failure

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

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.

Definitions

def self.default_root

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

Signature

returns String | Nil

The root directory of the project, or nil if it could not be determined.

Implementation

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

def self.for(exception)

Create a new failure event for the given exception.

Signature

parameter exception Exception

The exception to log.

Implementation

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

def self.log(subject, exception, **options)

Log a failure event with the given exception.

Signature

parameter subject String

The subject of the log message.

parameter exception Exception

The exception to log.

parameter options Hash

Additional options pass to the logger output.

Implementation

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

attr_reader :exception

Signature

attribute Exception

The exception which caused the failure.

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

Create a new failure event for the given exception.

Signature

parameter exception Exception

The exception to log.

parameter root String

The root directory of the project.

Implementation

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

def to_hash

Convert the failure event to a hash.

Signature

returns Hash

The hash representation of the failure event.

Implementation

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

def emit(*arguments, **options)

Log the failure event.

Signature

parameter arguments Array

The arguments to log.

parameter options Hash

Additional options to pass to the logger output.

Implementation

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