Skip to content

Commit

Permalink
catch exceptions in subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 27, 2016
1 parent a89a2d0 commit 27bce90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Mojo/IOLoop/Subprocess.pm
Expand Up @@ -26,7 +26,8 @@ sub run {
croak "Can't fork: $!" unless defined($self->{pid} = fork);
unless ($self->{pid}) {
$self->ioloop->reset;
print $writer $self->serialize->([$self->$first]);
my $results = eval { [$self->$first] } || [];
print $writer $self->serialize->([$@, @$results]);
exit 0;
}

Expand All @@ -40,7 +41,7 @@ sub run {
waitpid $self->{pid}, 0;
return $self->$second("Non-zero exit status (@{[$? >> 8]})") if $?;
my $result = eval { $self->deserialize->($buffer) } || [];
$self->$second($@, @$result);
$self->$second(shift(@$result) // $@, @$result);
}
);
return $self;
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/subprocess.t
Expand Up @@ -106,6 +106,18 @@ Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply $result, [], 'right structure';

# Exception
$fail = undef;
Mojo::IOLoop::Subprocess->new->run(
sub { die 'Whatever' },
sub {
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Mojo::IOLoop->start;
like $fail, qr/Whatever/, 'right error';

# Non-zero exit status
$fail = undef;
Mojo::IOLoop::Subprocess->new->run(
Expand Down

0 comments on commit 27bce90

Please sign in to comment.