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.



31
32
33
34
# File 'opal/opal/corelib/error.rb', line 31

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



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

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

.new(*args) ⇒ Object



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

def self.new(*args)
  %x{
    var message   = (args.length > 0) ? args[0] : nil;
    var error     = new self.$$constructor(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, stack_trace_limit);
    }

    return error;
  }
end

Instance Method Details

#backtraceObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'opal/opal/corelib/error.rb', line 36

def backtrace
  %x{
    if (self.backtrace) {
      // nil is a valid backtrace
      return self.backtrace;
    }

    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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'opal/opal/corelib/error.rb', line 56

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

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

#inspectObject



74
75
76
77
# File 'opal/opal/corelib/error.rb', line 74

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



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

def message
  to_s
end

#set_backtrace(backtrace) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'opal/opal/corelib/error.rb', line 79

def set_backtrace(backtrace)
  %x{
    var valid = true, i, ii;

    if (backtrace === nil) {
      self.backtrace = nil;
      self.stack = '';
    } else if (backtrace.$$is_string) {
      self.backtrace = [backtrace];
      self.stack = backtrace;
    } else {
      if (backtrace.$$is_array) {
        for (i = 0, ii = backtrace.length; i < ii; i++) {
          if (!backtrace[i].$$is_string) {
            valid = false;
            break;
          }
        }
      } else {
        valid = false;
      }

      if (valid === false) {
        #{raise TypeError, 'backtrace must be Array of String'}
      }

      self.backtrace = backtrace;
      self.stack = backtrace.join('\n');
    }

    return backtrace;
  }
end

#to_sObject



113
114
115
116
# File 'opal/opal/corelib/error.rb', line 113

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