Exception: Rhino::JSError

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

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ JSError

Returns a new instance of JSError.



5
6
7
8
9
10
11
12
# File 'lib/rhino/error.rb', line 5

def initialize(native)
  @native = native # NativeException wrapping a Java Throwable
  if ( value = self.value(true) ) != nil
    super value.is_a?(Exception) ? "#{value.class.name}: #{value.message}" : value
  else
    super cause ? cause.details : @native
  end
end

Instance Method Details

#backtraceObject

The backtrace is constructed using #javascript_backtrace + the Ruby part.



60
61
62
63
64
65
66
# File 'lib/rhino/error.rb', line 60

def backtrace
  if js_backtrace = javascript_backtrace
    js_backtrace.push(*super)
  else
    super
  end
end

#causeObject

Returns the (nested) cause of this error if any, most likely a #Rhino::JS::JavaScriptException instance.



26
27
28
29
30
31
32
33
# File 'lib/rhino/error.rb', line 26

def cause
  return @cause if defined?(@cause)
  if @native.respond_to?(:cause) && @native.cause
    @native.cause
  else
    @native.is_a?(JS::RhinoException) ? @native : nil
  end
end

#inspectObject



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

def inspect
  "#<#{self.class.name}: #{message}>"
end

#javascript_backtrace(keep_elements = false) ⇒ Object

Returns the JavaScript back-trace part for this error (the script stack).



69
70
71
72
73
74
75
76
77
# File 'lib/rhino/error.rb', line 69

def javascript_backtrace(keep_elements = false)
  if cause.is_a?(JS::RhinoException)
    cause.getScriptStack.map do |element| # ScriptStackElement[]
      keep_elements ? element : element.to_s
    end
  else
    nil
  end
end

#messageObject

Returns the error message, in case of a native JavaScript value, will return that value converted to a String.



20
21
22
# File 'lib/rhino/error.rb', line 20

def message
  super.to_s # since 1.9.x message is expected to allways be a string
end

#unwrapObject

Attempts to unwrap the (native) JavaScript/Java exception.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rhino/error.rb', line 36

def unwrap
  return @unwrap if defined?(@unwrap)
  cause = self.cause
  if cause && cause.is_a?(JS::WrappedException) 
    e = cause.getWrappedException
    if e && e.is_a?(Java::OrgJrubyExceptions::RaiseException)
      @unwrap = e.getException
    else
      @unwrap = e
    end
  else
    @unwrap = nil
  end
end

#value(unwrap = false) ⇒ Object

Return the thown (native) JavaScript value.



52
53
54
55
56
57
# File 'lib/rhino/error.rb', line 52

def value(unwrap = false)
  return @value if defined?(@value) && ! unwrap
  @value = get_value unless defined?(@value)
  return @value.unwrap if unwrap && @value.respond_to?(:unwrap)
  @value
end