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.



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

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



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

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
25
# 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;
    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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'opal/opal/corelib/error.rb', line 68

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").slice(0, 15));
    }
    else if (backtrace) {
      return self.backtrace = correct_backtrace(backtrace.slice(0, 15));
    }

    return [];
  }
end

#backtrace_locationsObject



88
89
90
91
92
93
94
95
96
# File 'opal/opal/corelib/error.rb', line 88

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



98
99
100
# File 'opal/opal/corelib/error.rb', line 98

def cause
  `self.cause || nil`
end

#exception(str = nil) ⇒ Object



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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
# File 'opal/opal/corelib/error.rb', line 122

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



163
164
165
166
# File 'opal/opal/corelib/error.rb', line 163

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



118
119
120
# File 'opal/opal/corelib/error.rb', line 118

def message
  to_s
end

#set_backtrace(backtrace) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'opal/opal/corelib/error.rb', line 168

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



202
203
204
205
# File 'opal/opal/corelib/error.rb', line 202

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