Exception: V8::Error

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

Defined Under Namespace

Modules: Protect, Try

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, value, javascript_backtrace, cause = nil) ⇒ Error

Returns a new instance of Error.



24
25
26
27
28
29
# File 'lib/v8/error.rb', line 24

def initialize(message, value, javascript_backtrace, cause = nil)
  super(message)
  @value = value
  @cause = cause
  @javascript_backtrace = javascript_backtrace
end

Instance Attribute Details

#causeException (readonly)

Returns the underlying error (if any) that triggered this error to be raised.

Returns:

  • (Exception)

    the underlying error (if any) that triggered this error to be raised



14
15
16
# File 'lib/v8/error.rb', line 14

def cause
  @cause
end

#javascript_backtraceV8::StackTrace (readonly)

Returns the complete JavaScript stack at the point this error was thrown.

Returns:

  • (V8::StackTrace)

    the complete JavaScript stack at the point this error was thrown



18
19
20
# File 'lib/v8/error.rb', line 18

def javascript_backtrace
  @javascript_backtrace
end

#valueObject (readonly)

Returns the JavaScript value passed to the ‘throw` statement.

Returns:

  • (Object)

    the JavaScript value passed to the ‘throw` statement



10
11
12
# File 'lib/v8/error.rb', line 10

def value
  @value
end

Instance Method Details

#backtrace(*modifiers) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/v8/error.rb', line 41

def backtrace(*modifiers)
  return unless super()
  trace_framework = modifiers.include?(:framework)
  trace_ruby = modifiers.length == 0 || modifiers.include?(:ruby)
  trace_javascript = modifiers.length == 0 || modifiers.include?(:javascript)
  bilingual_backtrace(trace_ruby, trace_javascript).tap do |trace|
    trace.reject! {|frame| frame =~  %r{(lib/v8/.*\.rb|ext/v8/.*\.cc)}} unless modifiers.include?(:framework)
  end
end

#bilingual_backtrace(trace_ruby = true, trace_javascript = true) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/v8/error.rb', line 63

def bilingual_backtrace(trace_ruby = true, trace_javascript = true)
  backtrace = causes.reduce(:backtrace => [], :ruby => -1, :javascript => -1) { |accumulator, cause|
    accumulator.tap do
      if trace_ruby
        backtrace_selector = cause.respond_to?(:standard_error_backtrace) ? :standard_error_backtrace : :backtrace
        ruby_frames = cause.send(backtrace_selector)[0..accumulator[:ruby]]
        accumulator[:backtrace].unshift *ruby_frames
        accumulator[:ruby] -= ruby_frames.length
      end
      if trace_javascript && cause.respond_to?(:javascript_backtrace)
        javascript_frames = cause.javascript_backtrace.to_a[0..accumulator[:javascript]].map(&:to_s)
        accumulator[:backtrace].unshift *javascript_frames
        accumulator[:javascript] -= javascript_frames.length
      end
    end
  }[:backtrace]
end

#causesObject



31
32
33
34
35
36
37
38
39
# File 'lib/v8/error.rb', line 31

def causes
  [].tap do |causes|
    current = self
    until current.nil? do
      causes.push current
      current = current.respond_to?(:cause) ? current.cause : nil
    end
  end
end

#in_javascript?Boolean

Returns:

  • (Boolean)


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

def in_javascript?
  causes.last.is_a? self.class
end

#in_ruby?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/v8/error.rb', line 59

def in_ruby?
  !in_javascript?
end

#root_causeObject



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

def root_cause
  causes.last
end

#standard_error_backtraceObject

keep an alias to the StandardError#backtrace method so that we can capture just ruby backtrace frames



22
# File 'lib/v8/error.rb', line 22

alias_method :standard_error_backtrace, :backtrace