Class: Ferrum::Console::Message
- Inherits:
-
Object
- Object
- Ferrum::Console::Message
- Defined in:
- lib/ferrum/console/message.rb,
sig/ferrum/console/message.rbs
Overview
A message as the DevTools console would show it: a console.* call,
a browser log entry (failed resource loads, CSP violations,
deprecations, ...) or an uncaught exception. The text, arguments and
stack trace are only converted from CDP when asked for.
Constant Summary collapse
- SEVERITIES =
{ error: :error, assert: :error, warning: :warn, debug: :debug, verbose: :debug, trace: :debug }.freeze
- UNSERIALIZABLE =
{ "NaN" => Float::NAN, "Infinity" => Float::INFINITY, "-Infinity" => -Float::INFINITY, "-0" => -0.0 }.freeze
Instance Attribute Summary collapse
-
#source ⇒ Symbol
readonly
Where the message comes from:
:console_apiforconsole.*calls,:exceptionfor uncaught exceptions, otherwise the browser log entry's source such as:network,:security,:violation,:deprecationor:intervention. -
#timestamp ⇒ Time
readonly
When the message was logged.
-
#type ⇒ Symbol
readonly
What logged the message:
:log,:info,:warning,:error,:debug,:table,:trace,:assert, ...
Class Method Summary collapse
-
.from_console_api(params) ⇒ Message
Builds a message from
Runtime.consoleAPICalledparams. -
.from_exception(params) ⇒ Message
Builds a message from
Runtime.exceptionThrownparams. -
.from_log_entry(entry) ⇒ Message
Builds a message from a
Log.entryAddedentry.
Instance Method Summary collapse
-
#args ⇒ Array<Object>
The
console.*arguments as Ruby values. -
#error? ⇒ Boolean
Whether the message is an error:
console.error, a failedconsole.assert, an error log entry or an uncaught exception. -
#initialize(type:, source:, timestamp:, raw_args: nil, raw_text: nil, frames: nil) ⇒ Message
constructor
A new instance of Message.
-
#inspect ⇒ String
Debug representation of the message.
-
#location ⇒ Location?
Where the message was logged from.
-
#match?(pattern) ⇒ Boolean
Whether the message text matches the given pattern.
-
#severity ⇒ Symbol
The #type expressed as a
Loggermethod name, so a message can be passed on withlogger.public_send(message.severity, message). -
#stack_trace ⇒ Array<Location>
The call stack at the moment the message was logged, innermost frame first.
-
#text ⇒ String
The message text.
-
#to_s ⇒ String
The message as the logger prints it:
[type] text, followed by anat function (url:line:column)line per stack frame. -
#warning? ⇒ Boolean
Whether the message is a warning.
Constructor Details
#initialize(type:, source:, timestamp:, raw_args: nil, raw_text: nil, frames: nil) ⇒ Message
Returns a new instance of Message.
76 77 78 79 80 81 82 83 |
# File 'lib/ferrum/console/message.rb', line 76 def initialize(type:, source:, timestamp:, raw_args: nil, raw_text: nil, frames: nil) @type = underscore(type) @source = underscore(source) @timestamp = Time.at(0, .to_f, :millisecond) @raw_args = raw_args || [] @raw_text = raw_text @frames = frames || [] end |
Instance Attribute Details
#source ⇒ Symbol (readonly)
Where the message comes from: :console_api for console.* calls,
:exception for uncaught exceptions, otherwise the browser log
entry's source such as :network, :security, :violation,
:deprecation or :intervention.
34 35 36 |
# File 'lib/ferrum/console/message.rb', line 34 def source @source end |
#timestamp ⇒ Time (readonly)
When the message was logged.
39 40 41 |
# File 'lib/ferrum/console/message.rb', line 39 def @timestamp end |
#type ⇒ Symbol (readonly)
What logged the message: :log, :info, :warning, :error,
:debug, :table, :trace, :assert, ... for console.* calls,
:verbose, :info, :warning or :error for browser log entries
and :error for uncaught exceptions.
26 27 28 |
# File 'lib/ferrum/console/message.rb', line 26 def type @type end |
Class Method Details
.from_console_api(params) ⇒ Message
Builds a message from Runtime.consoleAPICalled params.
46 47 48 49 |
# File 'lib/ferrum/console/message.rb', line 46 def self.from_console_api(params) new(type: params["type"], source: "console_api", timestamp: params["timestamp"], raw_args: params["args"], frames: params.dig("stackTrace", "callFrames")) end |
.from_exception(params) ⇒ Message
Builds a message from Runtime.exceptionThrown params.
68 69 70 71 72 73 74 |
# File 'lib/ferrum/console/message.rb', line 68 def self.from_exception(params) details = params["exceptionDetails"] frames = details.dig("stackTrace", "callFrames") frames ||= [details.slice("url", "lineNumber", "columnNumber")] text = [details["text"], details.dig("exception", "description")].compact.uniq.join(" ") new(type: "error", source: "exception", timestamp: params["timestamp"], raw_text: text, frames: frames) end |
.from_log_entry(entry) ⇒ Message
Builds a message from a Log.entryAdded entry.
56 57 58 59 60 61 |
# File 'lib/ferrum/console/message.rb', line 56 def self.from_log_entry(entry) frames = entry.dig("stackTrace", "callFrames") frames ||= [entry.slice("url", "lineNumber")] if entry["url"] new(type: entry["level"], source: entry["source"], timestamp: entry["timestamp"], raw_text: entry["text"], frames: frames) end |
Instance Method Details
#args ⇒ Array<Object>
The console.* arguments as Ruby values. Strings, numbers,
booleans and null come as they are, undefined is nil, plain
objects and arrays are a Hash or an Array built from Chrome's
shallow preview, anything else (functions, DOM nodes, errors, ...) is
its description. Empty for log entries and exceptions.
100 101 102 |
# File 'lib/ferrum/console/message.rb', line 100 def args @args ||= @raw_args.map { |arg| arg_value(arg) } end |
#error? ⇒ Boolean
Whether the message is an error: console.error, a failed
console.assert, an error log entry or an uncaught exception.
132 133 134 |
# File 'lib/ferrum/console/message.rb', line 132 def error? severity == :error end |
#inspect ⇒ String
Debug representation of the message.
167 168 169 |
# File 'lib/ferrum/console/message.rb', line 167 def inspect "#<#{self.class.name} type=#{type.inspect} source=#{source.inspect} text=#{text.inspect}>" end |
#location ⇒ Location?
Where the message was logged from.
115 116 117 |
# File 'lib/ferrum/console/message.rb', line 115 def location stack_trace.first end |
#match?(pattern) ⇒ Boolean
Whether the message text matches the given pattern.
149 150 151 152 153 |
# File 'lib/ferrum/console/message.rb', line 149 def match?(pattern) return text.match?(pattern) if pattern.is_a?(Regexp) text.include?(pattern.to_s) end |
#severity ⇒ Symbol
The #type expressed as a Logger method name, so a message can be
passed on with logger.public_send(message.severity, message).
124 125 126 |
# File 'lib/ferrum/console/message.rb', line 124 def severity SEVERITIES.fetch(type, :info) end |
#stack_trace ⇒ Array<Location>
The call stack at the moment the message was logged, innermost frame first. Can be empty.
108 109 110 |
# File 'lib/ferrum/console/message.rb', line 108 def stack_trace @stack_trace ||= @frames.map { |frame| Location.from_frame(frame) } end |
#text ⇒ String
The message text. console.* arguments are joined by spaces, with
objects and arrays rendered as JSON.
89 90 91 |
# File 'lib/ferrum/console/message.rb', line 89 def text @text ||= @raw_text || @raw_args.map { |arg| arg_text(arg) }.join(" ") end |
#to_s ⇒ String
The message as the logger prints it: [type] text, followed by an
at function (url:line:column) line per stack frame.
159 160 161 162 |
# File 'lib/ferrum/console/message.rb', line 159 def to_s lines = stack_trace.map { |frame| " at #{frame.function_name} (#{frame})" } ["[#{type}] #{text}", *lines].join("\n") end |
#warning? ⇒ Boolean
Whether the message is a warning.
139 140 141 |
# File 'lib/ferrum/console/message.rb', line 139 def warning? severity == :warn end |