Skip to content

Commit

Permalink
Improve error messages when it evaluates expressions in Console
Browse files Browse the repository at this point in the history
  • Loading branch information
小野 直人 committed Nov 11, 2021
1 parent 2c87a24 commit 22a5845
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/debug/server_cdp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ def cdp_event args
result[:reason] = 'other'
@ui.fire_event 'Debugger.paused', **result
when :evaluate
res = result[:result]
[res].each {|e|
r = result.dig(:response, :result)
[r].each {|e|
if oid = e.dig(:objectId)
@scope_map[oid] = 'eval'
end
}
@ui.respond req, result: res
@ui.respond req, **result[:response]

out = result[:output]
if out && !out.empty?
Expand Down Expand Up @@ -420,18 +420,29 @@ def process_cdp args
}
}
when :evaluate
res = {}
expr = args.shift
begin
orig_stdout = $stdout
$stdout = StringIO.new
result = current_frame.binding.eval(expr.to_s, '(DEBUG CONSOLE)')
rescue Exception => e
result = e
b = e.backtrace.map{|e| " #{e}\n"}
line = b.first.match('.*:(\d+):in .*')[1].to_i
res[:exceptionDetails] = {
exceptionId: 1,
text: 'Uncaught',
lineNumber: line - 1,
columnNumber: 0,
exception: evaluate_result(e),
}
ensure
output = $stdout.string
$stdout = orig_stdout
end
event! :cdp_result, :evaluate, req, result: evaluate_result(result), output: output
res[:result] = evaluate_result(result)
event! :cdp_result, :evaluate, req, response: res, output: output
when :properties
oid = args.shift
if fid = @frame_id_map[oid]
Expand Down Expand Up @@ -476,7 +487,7 @@ def parse_object objs
end
end

def variable_ name, obj, type, description: nil, use_short: true
def variable_ name, obj, type, description: nil, subtype: nil, use_short: true
prop = {
name: name,
value: {
Expand All @@ -487,9 +498,11 @@ def variable_ name, obj, type, description: nil, use_short: true
configurable: true, # TODO: Change these parts because
enumerable: true # they are not necessarily `true`.
}
if description
if description && subtype
v = prop[:value]
v.delete :value
v[:description] = description
v[:subtype] = subtype
v[:objectId] = oid = rand.to_s
v[:className] = obj.class
@obj_map[oid] = obj
Expand All @@ -500,9 +513,9 @@ def variable_ name, obj, type, description: nil, use_short: true
def variable name, obj
case obj
when Array
variable_ name, obj, 'object', description: "Array(#{obj.size})"
variable_ name, obj, 'object', description: "Array(#{obj.size})", subtype: 'array'
when Hash
variable_ name, obj, 'object', description: 'Object'
variable_ name, obj, 'object', description: 'Object', subtype: 'map'
when Range, NilClass, Time
variable_ name, obj, 'object'
when String
Expand All @@ -517,6 +530,8 @@ def variable name, obj
variable_ name, obj, 'number'
when Integer
variable_ name, obj, 'number'
when Exception
variable_ name, obj, 'object', description: "#{obj.inspect}\n#{obj.backtrace.map{|e| " #{e}\n"}.join}", subtype: 'error'
else
variable_ name, obj, 'undefined'
end
Expand Down

0 comments on commit 22a5845

Please sign in to comment.