Exception: Exception

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

Overview

backtick_javascript: true use_strict: true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Exception

Returns a new instance of Exception.



37
38
39
40
# File 'opal/opal/corelib/error.rb', line 37

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



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

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

.new(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'opal/opal/corelib/error.rb', line 10

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;
    error.cause   = #{$!};
    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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'opal/opal/corelib/error.rb', line 83

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

    var backtrace = self.stack;

    if (typeof(backtrace) !== 'undefined' && backtrace.$$is_string) {
      return self.backtrace = correct_backtrace(backtrace.split("\n"));
    }
    else if (backtrace) {
      return self.backtrace = correct_backtrace(backtrace);
    }

    return [];
  }
end

#backtrace_locationsObject



103
104
105
106
107
108
109
110
111
# File 'opal/opal/corelib/error.rb', line 103

def backtrace_locations
  %x{
    if (self.backtrace_locations) return self.backtrace_locations;
    self.backtrace_locations = #{backtrace&.map do |loc|
      ::Thread::Backtrace::Location.new(loc)
    end}
    return self.backtrace_locations;
  }
end

#causeObject



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

def cause
  `self.cause || nil`
end

#copy_instance_variables(other) ⇒ Object

Those instance variables are not enumerable.



43
44
45
46
47
48
49
50
# File 'opal/opal/corelib/error.rb', line 43

def copy_instance_variables(other)
  super
  %x{
    self.message = other.message;
    self.cause = other.cause;
    self.stack = other.stack;
  }
end

#exception(str = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'opal/opal/corelib/error.rb', line 117

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

    var cloned = #{clone};
    cloned.message = str;
    if (self.backtrace) cloned.backtrace = self.backtrace.$dup();
    cloned.stack = self.stack;
    cloned.cause = self.cause;
    return cloned;
  }
end

#full_message(kwargs = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'opal/opal/corelib/error.rb', line 137

def full_message(kwargs = nil)
  unless defined? Hash
    # We are dealing with an unfully loaded Opal library, so we should
    # do with as little as we can.

    return "#{@message}\n#{`self.stack`}"
  end

  kwargs = { highlight: $stderr.tty?, order: :top }.merge(kwargs || {})
  highlight, order = kwargs[:highlight], kwargs[:order]

  ::Kernel.raise ::ArgumentError, "expected true or false as highlight: #{highlight}" unless [true, false].include? highlight
  ::Kernel.raise ::ArgumentError, "expected :top or :bottom as order: #{order}" unless %i[top bottom].include? order

  if highlight
    bold_underline = "\e[1;4m"
    bold = "\e[1m"
    reset = "\e[m"
  else
    bold_underline = bold = reset = ''
  end

  bt = backtrace.dup
  bt = caller if !bt || bt.empty?
  first = bt.shift

  msg = "#{first}: "
  msg += "#{bold}#{to_s} (#{bold_underline}#{self.class}#{reset}#{bold})#{reset}\n"

  msg += bt.map { |loc| "\tfrom #{loc}\n" }.join

  msg += cause.full_message(highlight: highlight) if cause

  if order == :bottom
    msg = msg.split("\n").reverse.join("\n")
    msg = "#{bold}Traceback#{reset} (most recent call last):\n" + msg
  end

  msg
end

#inspectObject



178
179
180
181
# File 'opal/opal/corelib/error.rb', line 178

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



133
134
135
# File 'opal/opal/corelib/error.rb', line 133

def message
  to_s
end

#set_backtrace(backtrace) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'opal/opal/corelib/error.rb', line 183

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 = '  from ' + 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) {
        #{::Kernel.raise ::TypeError, 'backtrace must be Array of String'}
      }

      self.backtrace = backtrace;
      self.stack = #{`backtrace`.map { |i| '  from ' + i }}.join("\n");
    }

    return backtrace;
  }
end

#to_sObject



217
218
219
220
# File 'opal/opal/corelib/error.rb', line 217

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