Exception: Exception
- Defined in:
- opal/opal/corelib/error.rb
Overview
backtick_javascript: true
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Exception
Returns a new instance of Exception.
36
37
38
39
|
# File 'opal/opal/corelib/error.rb', line 36
def initialize(*args)
`self.message = (args.length > 0) ? args[0] : nil`
end
|
Class Method Details
.exception(*args) ⇒ Object
32
33
34
|
# File 'opal/opal/corelib/error.rb', line 32
def self.exception(*args)
new(*args)
end
|
.new(*args) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'opal/opal/corelib/error.rb', line 9
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
#backtrace ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'opal/opal/corelib/error.rb', line 82
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_locations ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'opal/opal/corelib/error.rb', line 102
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
|
112
113
114
|
# File 'opal/opal/corelib/error.rb', line 112
def cause
`self.cause || nil`
end
|
#copy_instance_variables(other) ⇒ Object
Those instance variables are not enumerable.
42
43
44
45
46
47
48
49
|
# File 'opal/opal/corelib/error.rb', line 42
def copy_instance_variables(other)
super
%x{
self.message = other.message;
self.cause = other.cause;
self.stack = other.stack;
}
end
|
#exception(str = nil) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'opal/opal/corelib/error.rb', line 116
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'opal/opal/corelib/error.rb', line 136
def full_message(kwargs = nil)
unless defined? Hash
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
|
177
178
179
180
|
# File 'opal/opal/corelib/error.rb', line 177
def inspect
as_str = to_s
as_str.empty? ? self.class.to_s : "#<#{self.class.to_s}: #{to_s}>"
end
|
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
132
133
134
|
# File 'opal/opal/corelib/error.rb', line 132
def message
to_s
end
|
#set_backtrace(backtrace) ⇒ Object
182
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
|
# File 'opal/opal/corelib/error.rb', line 182
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
|
216
217
218
219
|
# File 'opal/opal/corelib/error.rb', line 216
def to_s
(@message && @message.to_s) || self.class.to_s
end
|