| 1 | use v5.40; | ||
| 2 | # | ||
| 3 | # Base class for cancellable-wait diagnostics. Thrown inside a fiber when a parked wait is | ||
| 4 | # interrupted; the object survives on the fiber's error slot and is what an awaiting parent sees | ||
| 5 | # via ->error. | ||
| 6 | package Acme::Parataxis::Error v0.1.6 { | ||
| 7 | use overload '""' => sub { $_[0]->message }, fallback => 1; | ||
| 8 | sub message ($self) { $self->{message} } | ||
| 9 | sub wait_reason ($self) { $self->{wait_reason} } # the [reason, file, line, backtrace] the interrupted wait parked with | ||
| 10 | |||
| 11 | # Shared constructor: blessed into the calling subclass. $label seeds the default message. | ||
| 12 | sub _new ( $class, $label, %args ) { | ||
| 13 | my $reason = delete $args{wait_reason}; | ||
| 14 | my $site = 'an unknown wait'; | ||
| 15 | $site = sprintf '%s (%s line %d)', @{$reason}[ 0 .. 2 ] if ref $reason eq 'ARRAY' && @$reason >= 3; | ||
| 16 | bless { label => $label, message => "$label at $site", wait_reason => $reason, %args }, $class; | ||
| 17 | } | ||
| 18 | |||
| 19 | # Thrown at the re-entry of a parked wait when an explicit cancellation token was cancelled while | ||
| 20 | # the fiber was waiting. | ||
| 21 | package Acme::Parataxis::Error::Cancelled v0.1.6 { | ||
| 22 | use parent 'Acme::Parataxis::Error'; | ||
| 23 | sub new ( $class, %args ) { Acme::Parataxis::Error::_new( $class, 'The operation was cancelled', %args ) } | ||
| 24 | sub throw ( $class, %args ) { die $class->new(%args) } | ||
| 25 | sub kind ($self) {'cancelled'} | ||
| 26 | } | ||
| 27 | |||
| 28 | # Thrown at the re-entry of a parked wait when a with_timeout deadline fired before the block was | ||
| 29 | # able to finish. | ||
| 30 | package Acme::Parataxis::Error::Timeout v0.1.6 { | ||
| 31 | use parent 'Acme::Parataxis::Error'; | ||
| 32 | |||
| 33 | sub new ( $class, %args ) { | ||
| 34 | my $seconds = delete $args{seconds}; | ||
| 35 | my $label = defined $seconds ? "The operation timed out after ${seconds}s" : 'The operation timed out'; | ||
| 36 | Acme::Parataxis::Error::_new( $class, $label, seconds => $seconds, %args ); | ||
| 37 | } | ||
| 38 | sub throw ( $class, %args ) { die $class->new(%args) } | ||
| 39 | sub kind ($self) {'timeout'} | ||
| 40 | sub seconds ($self) { $self->{seconds} } | ||
| 41 | } | ||
| 42 | |||
| 43 | # Thrown by nursery() when one or more enrolled children died. The complete failure list | ||
| 44 | # is reachable via ->failures; ->primary is the first *non-cancellation* failure (i.e. | ||
| 45 | # the real culprit, not the siblings it brought down). Plain-string dies survive too. | ||
| 46 | package Acme::Parataxis::Error::Nursery v0.1.6 { | ||
| 47 | use parent 'Acme::Parataxis::Error'; | ||
| 48 | |||
| 49 | sub new ( $class, %args ) { | ||
| 50 | my $failures = delete $args{failures}; | ||
| 51 | $failures = [] unless ref $failures eq 'ARRAY'; | ||
| 52 | my @natural = grep { | ||
| 53 | my $k; | ||
| 54 | try { $k = $_->kind } catch ($e) { | ||
| 55 | } # plain string or object without ->kind | ||
| 56 | !defined($k) || $k ne 'cancelled' | ||
| 57 | } @$failures; | ||
| 58 | my $primary = $natural[0] // $failures->[0]; | ||
| 59 | my $message = defined $primary ? "nursery failure: $primary" : 'nursery failure'; | ||
| 60 | bless { message => $message, label => 'nursery failure', wait_reason => undef, failures => $failures, primary => $primary, %args }, | ||
| 61 | $class; | ||
| 62 | } | ||
| 63 | sub kind ($self) {'nursery'} | ||
| 64 | sub failures ($self) { @{ $self->{failures} // [] } } # every child's error, in spawn order | ||
| 65 | sub primary ($self) { $self->{primary} } # first natural (non-cancelled) failure | ||
| 66 | } | ||
| 67 | |||
| 68 | # Thrown by Acme::Parataxis::Supervisor->run when a supervised tree exhausts its restart | ||
| 69 | # budget. Same shape as Error::Nursery: ->failures is every child death that counted against | ||
| 70 | # the budget (in the order they happened), ->primary is the one that blew it, and ->child names | ||
| 71 | # that child. The tree is already stopped by the time this is thrown. | ||
| 72 | package Acme::Parataxis::Error::Supervisor v0.1.6 { | ||
| 73 | use parent 'Acme::Parataxis::Error'; | ||
| 74 | |||
| 75 | sub new ( $class, %args ) { | ||
| 76 | my $failures = delete $args{failures}; | ||
| 77 | $failures = [] unless ref $failures eq 'ARRAY'; | ||
| 78 | my $child = delete $args{child}; | ||
| 79 | my $primary = delete $args{primary}; | ||
| 80 | $primary //= ( grep {defined} @$failures )[0]; | ||
| 81 | my $label = 'supervisor failure'; | ||
| 82 | my $message = 'supervisor failure: restart budget exhausted'; | ||
| 83 | $message .= " by child '$child'" if defined $child; | ||
| 84 | $message .= ": $primary" if defined $primary; | ||
| 85 | bless { message => $message, label => $label, wait_reason => undef, failures => $failures, primary => $primary, child => $child, %args }, | ||
| 86 | $class; | ||
| 87 | } | ||
| 88 | sub kind ($self) {'supervisor'} | ||
| 89 | sub failures ($self) { @{ $self->{failures} // [] } } # every death that counted against the budget | ||
| 90 | sub primary ($self) { $self->{primary} } # the death that exhausted it | ||
| 91 | sub child ($self) { $self->{child} } # name of that child | ||
| 92 | } | ||
| 93 | |||
| 94 | # Control flow for STM, not a user-facing error: thrown by retry() inside an | ||
| 95 | # atomically block and caught by atomically itself, which discards the transaction's | ||
| 96 | # writes, parks on the read set, and re-runs the block. It never escapes atomically - | ||
| 97 | # retry() outside a transaction croaks before this is constructed. | ||
| 98 | package Acme::Parataxis::Error::STM_Retry v0.1.6 { | ||
| 99 | use parent 'Acme::Parataxis::Error'; | ||
| 100 | sub new ( $class, %args ) { bless { label => 'STM retry', message => 'STM retry (internal)', %args }, $class } | ||
| 101 | sub kind ($self) {'stm_retry'} | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | # | ||
| 105 | 1; |