-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Failure.pm
66 lines (58 loc) · 2.08 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$PROCESS::FATAL = False;
my class Failure {
has $.exception;
has $!handled;
method new($exception) { self.bless(:$exception) }
# 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() is hidden_from_backtrace {
$!exception.throw unless $!handled
}
}
proto sub fail(|) is hidden_from_backtrace {*};
multi sub fail(Exception $e) is hidden_from_backtrace {
die $e if $*FATAL;
my $fail := Failure.new($e);
my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}
multi sub fail($payload) is hidden_from_backtrace {
die $payload if $*FATAL;
my $fail := Failure.new(X::AdHoc.new(:$payload));
my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}
multi sub fail(*@msg) is hidden_from_backtrace {
my $payload = @msg == 1 ?? @msg[0] !! @msg.join;
die $payload if $*FATAL;
my $fail := Failure.new(X::AdHoc.new(:$payload));
my Mu $return := nqp::getlexcaller('RETURN');
$return($fail) unless nqp::isnull($return);
$fail
}
multi sub die(Failure:D $failure) is hidden_from_backtrace {
$failure.exception.throw
}
multi sub die(Failure:U) is hidden_from_backtrace {
X::AdHoc('Failure').throw
}
# vim: ft=perl6 expandtab sw=4