-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Failure.pm
51 lines (42 loc) · 1.41 KB
/
Failure.pm
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
$PROCESS::FATAL = False;
my class Failure {
has $.exception;
has $!handled;
method new($ex) {
my $new = self.CREATE;
$new.BUILD($ex);
}
method BUILD($ex) {
$!exception = $ex;
self;
}
# TODO: should be Failure:D: multi just like method Bool,
# but obscure problems prevent us from making Mu.defined
# a multi. See http://irclog.perlgeek.de/perl6/2011-06-28#i_4016747
method defined() {
$!handled =1 if nqp::isconcrete(self);
Bool::False;
}
multi method Bool(Failure:D:) { $!handled = 1; Bool::False; }
method Int(Failure:D:) { $!handled ?? 0 !! $!exception.throw; }
method Num(Failure:D:) { $!handled ?? 0e0 !! $!exception.throw; }
method Numeric(Failure:D:) { $!handled ?? 0e0 !! $!exception.throw; }
multi method Str(Failure:D:) { $!handled ?? '' !! $!exception.throw; }
multi method gist(Failure:D:) { $!handled ?? $.perl !! $!exception.throw; }
Failure.^add_fallback(
-> $, $ { True },
method ($name) {
$!exception.throw;
}
);
method sink() { $!exception.throw unless $!handled }
}
my &fail := -> *@msg {
my $value = @msg == 1 ?? @msg[0] !! @msg.join('');
die $value if $*FATAL;
try die $value;
my $fail := Failure.new($!);
my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}