我有这个 perl 包:
package Mojo::Runner;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::IOLoop::Subprocess;
use Mojo::Promise;
use IPC::Run;
sub run_p {
my ($self, $command, $stdin) = @_;
my ($stdout, $stderr);
my $subprocess = Mojo::IOLoop::Subprocess->new;
return $subprocess->run_p(sub {
my ($stdin, $stdout, $stderr);
my $process = IPC::Run::run $command, \$stdin, \$stdout, \$stderr;
my $e = $?;
my $promise = Mojo::Promise->new;
return $promise->reject($stderr) if $e;
return $stdout
})
}
1
它应该在 Mojo::IOLoop::Subprocess 内部使用 IPC::Run 运行某个进程,这样我就可以按如下方式使用它:
use FindBin qw($Bin);
use lib "$Bin/./lib";
use Mojo::Runner;
use strict;
$\ = "\n"; $, = "\t";
my $r = Mojo::Runner->new;
my $subprocess = $r->run_p(["rclone", "lsl", "my_own:", sprintf "--drive-root-folder-id=%s", "some_id" ])
->then(sub {
print $_ =~ s/\n$//msr for @_
})
->catch(sub {
my $err = shift;
print "Subprocess error: $err";
});
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;
现在,当我故意使其失败时,例如通过使用错误的命令语法,我会收到一个错误:
Unhandled rejected promise: 2024/10/01 17:25:57 Failed to lsl: couldn't list directory: googleapi: Error 404: File not found: ., notFound
at /Users/simone/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0/Mojo/IOLoop/Subprocess.pm line 55.
如果IPC::Run进程返回ok,则一切正常。
为什么未处理承诺?我需要改变什么?