class Console::Event::Failure
Generally, you should use the {Console.error} method to log failures, as it will automatically create a failure event for you.
“‘
end
Console::Event::Failure.log(“Something went wrong!”, exception)
rescue => exception
raise “Something went wrong!”
begin
“`ruby
Represents a failure of some kind, usually with an attached exception.
def self.default_root
For the purpose of efficiently formatting backtraces, we need to know the root directory of the project.
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.
def self.for(exception) self.new(exception, self.default_root) end
def self.log(subject, exception, **options)
@parameter exception [Exception] The exception to log.
@parameter subject [String] The subject of the log message.
Log a failure event with the given exception.
def self.log(subject, exception, **options) Console.error(subject, **self.for(exception).to_hash, **options) end
def emit(*arguments, **options)
@parameter arguments [Array] The arguments to log.
Log the failure event.
def emit(*arguments, **options) options[:severity] ||= :error super end
def extract(exception, hash)
def extract(exception, hash) hash[:class] = exception.class.name if exception.respond_to?(:detailed_message) message = exception.detailed_message # We want to remove the trailling exception class as we format it differently: message.sub!(/\s*\(.*?\)$/, "") hash[:message] = message else hash[:message] = exception.message end hash[:backtrace] = exception.backtrace if cause = exception.cause hash[:cause] = Hash.new.tap do |cause_hash| extract(cause, cause_hash) end end end
def initialize(exception, root = self.class.default_root)
@parameter exception [Exception] The exception to log.
Create a new failure event for the given exception.
def initialize(exception, root = self.class.default_root) @exception = exception @root = root end
def to_hash
Convert the failure event to a hash.
def to_hash Hash.new.tap do |hash| hash[:type] = :failure hash[:root] = @root if @root extract(@exception, hash) end end