forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.rb
578 lines (462 loc) · 12.1 KB
/
exception.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
112
113
114
115
116
117
118
119
120
121
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
162
163
164
165
166
167
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
class Exception
attr_accessor :locations
attr_accessor :cause
attr_accessor :custom_backtrace
def initialize(message = nil)
@reason_message = message
@locations = nil
@backtrace = nil
@custom_backtrace = nil
end
private :initialize
def capture_backtrace!(offset=1)
@locations = Rubinius::VM.backtrace offset
end
def ==(other)
other.instance_of?(__class__) &&
message == other.message &&
backtrace == other.backtrace
end
def to_s
if @reason_message
@reason_message.to_s
else
self.class.to_s
end
end
# This is here rather than in yaml.rb because it contains "private"
# information, ie, the list of ivars. Putting it over in the yaml
# source means it's easy to forget about.
def to_yaml_properties
list = super
list.delete :@backtrace
list.delete :@custom_backtrace
return list
end
def message
@reason_message
end
# Needed to properly implement #exception, which must clone and call
# #initialize again, BUT not a subclasses initialize.
alias_method :__initialize__, :initialize
def backtrace
return @custom_backtrace if @custom_backtrace
if backtrace?
awesome_backtrace.to_mri
else
nil
end
end
# Indicates if the Exception has a backtrace set
def backtrace?
(@backtrace || @locations) ? true : false
end
def awesome_backtrace
@backtrace ||= Rubinius::Backtrace.backtrace(@locations)
end
def render(header="An exception occurred", io=STDERR, color=true)
exception = self
exceptions = []
while exception
exceptions.unshift exception
exception = exception.cause
end
while exception = exceptions.shift
if custom_trace = exception.custom_backtrace
io.puts "User-defined backtrace:", "\n"
custom_trace.reverse_each { |line| io.puts line }
io.puts "\nBacktrace:", "\n"
end
io.puts exception.awesome_backtrace.show("\n", color)
io.puts "\n#{exception.message} (#{exception.class})"
io.puts "\nCausing:", "\n" unless exceptions.empty?
end
message_lines = message.to_s.split("\n")
io.puts header
io.puts
io.puts " #{message_lines.shift} (#{self.class})"
message_lines.each do |line|
io.puts " #{line}"
end
end
def set_backtrace(bt)
if bt.kind_of? Rubinius::Backtrace
@backtrace = bt
else
# See if we stashed a Backtrace object away, and use it.
if hidden_bt = Rubinius::Backtrace.detect_backtrace(bt)
@backtrace = hidden_bt
else
type_error = TypeError.new "backtrace must be Array of String"
case bt
when Array
if bt.all? { |s| s.kind_of? String }
@custom_backtrace = bt
else
raise type_error
end
when String
@custom_backtrace = [bt]
when nil
@custom_backtrace = nil
else
raise type_error
end
end
end
end
# This is important, because I subclass can just override #to_s and calling
# #message will call it. Using an alias doesn't achieve that.
def message
to_s
end
def inspect
s = self.to_s
if s.empty?
self.class.name
else
"#<#{self.class.name}: #{s}>"
end
end
class << self
alias_method :exception, :new
end
def exception(message=nil)
if message
unless message.equal? self
# As strange as this might seem, this IS actually the protocol
# that MRI implements for this. The explicit call to
# Exception#initialize (via __initialize__) is exactly what MRI
# does.
e = clone
Rubinius.privately { e.__initialize__(message) }
return e
end
end
self
end
def location
[context.file.to_s, context.line]
end
end
class PrimitiveFailure < Exception
end
class ScriptError < Exception
end
class StandardError < Exception
end
class SignalException < Exception
end
class NoMemoryError < Exception
end
class ZeroDivisionError < StandardError
end
class ArgumentError < StandardError
def to_s
if @given and @expected
if @method_name
"method '#{@method_name}': given #{@given}, expected #{@expected}"
else
"given #{@given}, expected #{@expected}"
end
else
super
end
end
end
class UncaughtThrowError < ArgumentError
attr_accessor :tag
end
class IndexError < StandardError
end
class StopIteration < IndexError
end
class RangeError < StandardError
end
class FloatDomainError < RangeError
end
class LocalJumpError < StandardError
end
class NameError < StandardError
attr_reader :name
def initialize(*args, receiver: nil)
super(args.shift)
@name = args.shift
@receiver = receiver
end
private :initialize
def receiver
if @receiver
@receiver
else
raise ArgumentError, 'no receiver is available'
end
end
end
class NoMethodError < NameError
attr_reader :name
attr_reader :args
def initialize(*arguments, **options)
super(arguments.shift, **options)
@name = arguments.shift
@args = arguments.shift
end
private :initialize
end
class RuntimeError < StandardError
end
class SecurityError < StandardError
end
class ThreadError < StandardError
end
class FiberError < StandardError
end
class TypeError < StandardError
end
class FloatDomainError < RangeError
end
class RegexpError < StandardError
end
class LoadError < ScriptError
attr_accessor :path
def initialize(message=nil, path: nil)
super message
@path = path
end
private :initialize
class InvalidExtensionError < LoadError
end
class MRIExtensionError < InvalidExtensionError
end
end
class NotImplementedError < ScriptError
end
class Interrupt < SignalException
def initialize(*args)
super(args.shift)
@name = args.shift
end
private :initialize
end
class IOError < StandardError
end
class EOFError < IOError
end
class LocalJumpError < StandardError
end
class SyntaxError < ScriptError
attr_accessor :column
attr_accessor :line
attr_accessor :file
attr_accessor :code
def self.from(message, column, line, code, file)
exc = new message
exc.file = file
exc.line = line
exc.column = column
exc.code = code
exc
end
def reason
@reason_message
end
end
class SystemExit < Exception
##
# Process exit status if this exception is raised
attr_reader :status
##
# Creates a SystemExit exception with optional status and message. If the
# status is omitted, Process::EXIT_SUCCESS is used.
#--
# *args is used to simulate optional prepended argument like MRI
def initialize(first=nil, *args)
if first.kind_of?(Fixnum)
status = first
super(*args)
else
status = Process::EXIT_SUCCESS
super
end
@status = status
end
private :initialize
##
# Returns true is exiting successfully, false if not. A successful exit is
# one with a status equal to 0 (zero). Any other status is considered a
# unsuccessful exit.
def success?
status == Process::EXIT_SUCCESS
end
end
class SystemCallError < StandardError
attr_reader :errno
def self.errno_error(message, errno, location)
Rubinius.primitive :exception_errno_error
raise PrimitiveFailure, "SystemCallError.errno_error failed"
end
# We use .new here because when errno is set, we attempt to
# lookup and return a subclass of SystemCallError, specificly,
# one of the Errno subclasses.
def self.new(*args)
# This method is used 2 completely different ways. One is when it's called
# on SystemCallError, in which case it tries to construct a Errno subclass
# or makes a generic instead of itself.
#
# Otherwise it's called on a Errno subclass and just helps setup
# a instance of the subclass
if self.equal? SystemCallError
case args.size
when 1
if args.first.kind_of?(Fixnum)
errno = args.first
message = nil
else
errno = nil
message = StringValue(args.first)
end
location = nil
when 2
message, errno = args
location = nil
when 3
message, errno, location = args
else
raise ArgumentError, "wrong number of arguments (#{args.size} for 1..3)"
end
# If it corresponds to a known Errno class, create and return it now
if errno && error = SystemCallError.errno_error(message, errno, location)
return error
else
return super(message, errno, location)
end
else
case args.size
when 0
message = nil
location = nil
when 1
message = StringValue(args.first)
location = nil
when 2
message, location = args
else
raise ArgumentError, "wrong number of arguments (#{args.size} for 0..2)"
end
if defined?(self::Errno) && self::Errno.kind_of?(Fixnum)
errno = self::Errno
error = SystemCallError.errno_error(message, self::Errno, location)
if error && error.class.equal?(self)
return error
end
end
error = allocate
Rubinius::Unsafe.set_class error, self
Rubinius.privately { error.initialize(*args) }
return error
end
end
# Must do this here because we have a unique new and otherwise .exception will
# call Exception.new because of the alias in Exception.
class << self
alias_method :exception, :new
end
# Use splat args here so that arity returns -1 to match MRI.
def initialize(*args)
kls = self.class
message, errno, location = args
@errno = errno
msg = "unknown error"
msg << " @ #{StringValue(location)}" if location
msg << " - #{StringValue(message)}" if message
super(msg)
end
private :initialize
end
class KeyError < IndexError
end
class SignalException < Exception
attr_reader :signo
attr_reader :signm
def initialize(signo = nil, signm = nil)
# MRI overrides this behavior just for SignalException itself
# but not for anything that inherits from it, therefore we
# need this ugly check to make sure it works as intented.
return super(signo) unless self.class == SignalException
if signo.is_a? Integer
unless @signm = Signal::Numbers[signo]
raise ArgumentError, "invalid signal number #{signo}"
end
@signo = signo
@signm = signm || "SIG#{@signm}"
elsif signo
if signm
raise ArgumentError, "wrong number of arguments (2 for 1)"
end
signm = signo
if signo.kind_of?(Symbol)
signm = signm.to_s
else
signm = StringValue(signm)
end
signm = signm[3..-1] if signm.prefix? "SIG"
unless @signo = Signal::Names[signm]
raise ArgumentError, "invalid signal name #{signm}"
end
@signm = "SIG#{signm}"
end
super(@signm)
end
private :initialize
end
class StopIteration
attr_accessor :result
private :result=
end
##
# Base class for various exceptions raised in the VM.
class Rubinius::MachineException < Exception
end
class Rubinius::ConcurrentUpdateError < Rubinius::MachineException
end
class Rubinius::VMException < Exception
end
##
# Raised in the VM when an assertion fails.
class Rubinius::AssertionError < Rubinius::VMException
end
##
# Raised in the VM when attempting to read/write outside
# the bounds of an object.
class Rubinius::ObjectBoundsExceededError < Rubinius::VMException
end
# Defined by the VM itself
class Rubinius::InvalidBytecode < Rubinius::Internal
attr_reader :compiled_code
attr_reader :ip
def message
if @compiled_code
if @ip and @ip >= 0
"#{super} - at #{@compiled_code.name}+#{@ip}"
else
"#{super} - method #{@compiled_code.name}"
end
else
super
end
end
end
class InterpreterError < Exception
end
class DeadlockError < Exception
end
# MRI has an Exception class named "fatal" that is raised
# by the rb_fatal function. The class is not accessible from
# ruby because the name is begins with a lower-case letter.
# Also, the exception cannot be rescued.
#
# To support rb_fatal in the C-API, Rubinius provides the
# following FatalError class. If it clashes with code in
# the wild, we can rename it.
class FatalError < Exception
end