Exception: Exception

Defined in:
opal/opal/corelib/error.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Exception

Returns a new instance of Exception



28
29
30
31
# File 'opal/opal/corelib/error.rb', line 28

def initialize(*args)
  # using self.message aka @message to retain compatibility with native exception's message property
  `self.message = (args.length > 0) ? args[0] : nil`
end

Class Method Details

.exception(*args) ⇒ Object



24
25
26
# File 'opal/opal/corelib/error.rb', line 24

def self.exception(*args)
  new(*args)
end

.new(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'opal/opal/corelib/error.rb', line 3

def self.new(*args)
  %x{
    var message   = (args.length > 0) ? args[0] : nil;
    var error     = new self.$$alloc(message);
    error.name    = self.$$name;
    error.message = message;
    Opal.send(error, error.$initialize, args);

    // Error.captureStackTrace() will use .name and .toString to build the
    // first line of the stack trace so it must be called after the error
    // has been initialized.
    // https://nodejs.org/dist/latest-v6.x/docs/api/errors.html
    if (Opal.config.enable_stack_trace && Error.captureStackTrace) {
      // Passing Kernel.raise will cut the stack trace from that point above
      Error.captureStackTrace(error, Kernel$raise);
    }

    return error;
  }
end

Instance Method Details

#backtraceObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'opal/opal/corelib/error.rb', line 33

def backtrace
  %x{
    var backtrace = self.stack;

    if (typeof(backtrace) === 'string') {
      return backtrace.split("\n").slice(0, 15);
    }
    else if (backtrace) {
      return backtrace.slice(0, 15);
    }

    return [];
  }
end

#exception(str = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'opal/opal/corelib/error.rb', line 48

def exception(str=nil)
  %x{
    if (str === nil || self === str) {
      return self;
    }

    var cloned = #{self.clone};
    cloned.message = str;
    return cloned;
  }
end

#inspectObject



65
66
67
68
# File 'opal/opal/corelib/error.rb', line 65

def inspect
  as_str = to_s
  as_str.empty? ? self.class.to_s : "#<#{self.class.to_s}: #{to_s}>"
end

#messageObject

not using alias message to_s because you need to be able to override to_s and have message use overridden method, won't work with alias



61
62
63
# File 'opal/opal/corelib/error.rb', line 61

def message
  to_s
end

#to_sObject



70
71
72
73
# File 'opal/opal/corelib/error.rb', line 70

def to_s
  # using self.message aka @message to retain compatibility with native exception's message property
  (@message && @message.to_s) || self.class.to_s
end