Skip to content

Commit 3e3b662

Browse files
authored
Added Hyperf\Command\Listener\FailToHandleListener. (#4723)
1 parent 9fd4143 commit 3e3b662

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/Command.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
429429
throw $exception;
430430
}
431431

432+
$this->output && $this->error($exception->getMessage());
433+
432434
$this->eventDispatcher->dispatch(new Event\FailToHandle($this, $exception));
433435
return $this->exitCode = $exception->getCode();
434436
} finally {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Command\Listener;
13+
14+
use Hyperf\Command\Event\FailToHandle;
15+
use Hyperf\Contract\StdoutLoggerInterface;
16+
use Hyperf\Event\Contract\ListenerInterface;
17+
use Psr\Container\ContainerInterface;
18+
19+
class FailToHandleListener implements ListenerInterface
20+
{
21+
public function __construct(private ContainerInterface $container)
22+
{
23+
}
24+
25+
public function listen(): array
26+
{
27+
return [
28+
FailToHandle::class,
29+
];
30+
}
31+
32+
/**
33+
* @param FailToHandle $event
34+
*/
35+
public function process(object $event): void
36+
{
37+
if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
38+
$logger->error((string) $event->getThrowable());
39+
}
40+
}
41+
}

tests/CommandTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ public function testExitCodeWhenThrowException()
5757
$command = new ClassInvoker(new FooExceptionCommand('foo'));
5858
$input = Mockery::mock(InputInterface::class);
5959
$input->shouldReceive('getOption')->andReturnFalse();
60-
$exitCode = $command->execute($input, Mockery::mock(OutputInterface::class));
60+
$output = Mockery::mock(OutputInterface::class);
61+
$output->shouldReceive('writeln')->withAnyArgs()->andReturnNull();
62+
63+
$exitCode = $command->execute($input, $output);
6164
$this->assertSame(99, $exitCode);
6265

6366
/** @var FooExitCommand $command */
6467
$command = new ClassInvoker(new FooExitCommand());
65-
$exitCode = $command->execute($input, Mockery::mock(OutputInterface::class));
68+
$exitCode = $command->execute($input, $output);
6669
$this->assertSame(11, $exitCode);
6770

6871
/** @var FooCommand $command */
6972
$command = new ClassInvoker(new FooCommand());
70-
$exitCode = $command->execute($input, Mockery::mock(OutputInterface::class));
73+
$exitCode = $command->execute($input, $output);
7174
$this->assertSame(0, $exitCode);
7275
}
7376

0 commit comments

Comments
 (0)