Exception: V8::JSError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/v8/error.rb

Defined Under Namespace

Classes: Boundary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(try, to) ⇒ JSError

Returns a new instance of JSError.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/v8/error.rb', line 6

def initialize(try, to)
  @to = to
  begin
    super(initialize_unsafe(try))
  rescue Exception => e
    @boundaries = [Boundary.new(:rbframes => e.backtrace)]
    @value = e
    super(<<-EOMSG)
You have uncovered what is probably a BUG in therubyracer exception code. An error report would be very helpful.
JSError#initialize failed!: #{e.message}"
EOMSG
  end
end

Instance Attribute Details

#boundariesObject (readonly)

Returns the value of attribute boundaries.



4
5
6
# File 'lib/v8/error.rb', line 4

def boundaries
  @boundaries
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/v8/error.rb', line 4

def value
  @value
end

Instance Method Details

#backtrace(*modifiers) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/v8/error.rb', line 59

def backtrace(*modifiers)
  trace_framework = modifiers.include?(:framework)
  trace_ruby = modifiers.length == 0 || modifiers.include?(:ruby)
  trace_javascript = modifiers.length == 0 || modifiers.include?(:javascript)
  mixed = []
  rbcontext = []
  jscontext = []
  @boundaries.each do |b|
    rbframes = b.rbframes.dup
    rbcontext.reverse_each do |frame|
      if frame == rbframes.last
        rbframes.pop
      else
        break
      end
    end if trace_ruby
    jsframes = b.jsframes.dup
    jscontext.reverse_each do |frame|
      if frame == jsframes.last
        jsframes.pop
      else
        break
      end
    end if trace_javascript
    rbcontext = b.rbframes
    jscontext = b.jsframes
    rbframes.reject! {|f| f =~ /lib\/v8\/.*\.rb/} unless trace_framework
    mixed.unshift(*rbframes) if trace_ruby
    mixed.unshift(*jsframes) if trace_javascript
  end
  return mixed
end

#in_javascript?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/v8/error.rb', line 55

def in_javascript?
  !in_ruby?
end

#in_ruby?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/v8/error.rb', line 51

def in_ruby?
  @value.kind_of?(Exception)
end

#initialize_unsafe(try) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/v8/error.rb', line 20

def initialize_unsafe(try)
  message = nil
  ex = @to.rb(try.Exception())
  @boundaries = [Boundary.new(:rbframes => caller(3), :jsframes => parse_js_frames(try))]
  if V8::Object === ex
    if msg = ex['message']
      message = msg
    else
      message = ex.to_s
    end
    if cause = ex.instance_variable_get(:@native).GetHiddenValue("TheRubyRacer::Cause")
      if !cause.IsEmpty()
        prev = cause.Value()
        if prev.kind_of?(JSError)
          @boundaries.concat prev.boundaries
          @value = prev.value
        else
          @value = prev
          @boundaries.concat [Boundary.new(:rbframes => prev.backtrace)]
        end
      else
        @value = ex
      end
    end
  else
    @value = ex
    message = ex.to_s
  end
  return message
end

#parse_js_frames(try) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/v8/error.rb', line 92

def parse_js_frames(try)
  #I can't figure out why V8 is not capturing the stacktrace here
  #in terms of StackTrace and StackFrame objects, so we have to
  #parse the string.
  raw = @to.rb(try.StackTrace())
  if raw && !raw.empty? && !syntax_error?(try)
    raw.split("\n")[1..-1].tap do |frames|
      frames.each {|frame| frame.strip!; frame.chomp!(",")}
    end.reject(&:empty?)
  else
    msg = try.Message()
    ["at #{@to.rb(msg.GetScriptResourceName())}:#{msg.GetLineNumber()}:#{msg.GetStartColumn() + 1}"]
  end
end

#syntax_error?(try) ⇒ Boolean

Syntax errors are weird in that they have a non-empty stack trace but it does not contain any source location information, so in these instances, we have to pull it out of the Message object in the TryCatch. Is there a better way to detect a syntax error

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/v8/error.rb', line 111

def syntax_error?(try)
  ex = @to.rb(try.Exception())
  if ex && ex.kind_of?(V8::Object)
    type = ex["constructor"]
    type && type.kind_of?(V8::Function) && type.name == "SyntaxError"
  else
    false
  end
end