Class: Ferrum::Console::Message

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (Hash[Symbol, Symbol])
{ error: :error, assert: :error, warning: :warn,
debug: :debug, verbose: :debug, trace: :debug }.freeze
UNSERIALIZABLE =

Returns:

  • (Hash[String, ::Float])
{ "NaN" => Float::NAN, "Infinity" => Float::INFINITY,
"-Infinity" => -Float::INFINITY, "-0" => -0.0 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, source:, timestamp:, raw_args: nil, raw_text: nil, frames: nil) ⇒ Message

Returns a new instance of Message.

Parameters:

  • type: (String)
  • source: (String)
  • timestamp: (::Numeric, nil)
  • raw_args: (Array[Hash[String, untyped]], nil) (defaults to: nil)
  • raw_text: (String, nil) (defaults to: nil)
  • frames: (Array[Hash[String, untyped]], nil) (defaults to: nil)


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, timestamp.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.

Returns:

  • (Symbol)


34
35
36
# File 'lib/ferrum/console/message.rb', line 34

def source
  @source
end

#timestamp ⇒ Time (readonly)

When the message was logged.

Returns:

  • (Time)


39
40
41
# File 'lib/ferrum/console/message.rb', line 39

def timestamp
  @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.

Returns:

  • (Symbol)


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.

Parameters:

  • params (Hash{String => Object})

Returns:



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.

Parameters:

  • params (Hash{String => Object})

Returns:



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.

Parameters:

  • entry (Hash{String => Object})

Returns:



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.

Returns:

  • (Array<Object>)


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.

Returns:

  • (Boolean)


132
133
134
# File 'lib/ferrum/console/message.rb', line 132

def error?
  severity == :error
end

#inspect ⇒ String

Debug representation of the message.

Returns:

  • (String)


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.

Returns:



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.

Parameters:

  • pattern (Regexp, String) —

    A regexp matched against the text, or a string checked for inclusion in it.

Returns:

  • (Boolean)


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).

Returns:

  • (Symbol) —

    :debug, :info, :warn or :error.



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.

Returns:



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.

Returns:

  • (String)


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.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


139
140
141
# File 'lib/ferrum/console/message.rb', line 139

def warning?
  severity == :warn
end