Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ 11.x ] Adds ability to manually fail a command from outside the handle() method #51435

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class Command extends SymfonyCommand
{
Expand Down Expand Up @@ -210,6 +211,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

try {
return (int) $this->laravel->call([$this, $method]);
} catch (ManuallyFailedException $e) {
$this->components->error($e->getMessage());

return static::FAILURE;
} finally {
if ($this instanceof Isolatable && $this->option('isolated') !== false) {
$this->commandIsolationMutex()->forget($this);
Expand Down Expand Up @@ -254,6 +259,25 @@ protected function resolveCommand($command)
return $command;
}

/**
* Fail the command manually.
*
* @param \Throwable|string|null $exception
* @return void
*/
public function fail(Throwable|string|null $exception = null)
{
if (is_null($exception)) {
$exception = 'Command failed manually.';
}

if (is_string($exception)) {
$exception = new ManuallyFailedException($exception);
}

throw $exception;
}

/**
* {@inheritdoc}
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Console/ManuallyFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Console;

use RuntimeException;

class ManuallyFailedException extends RuntimeException
{
//
}
13 changes: 10 additions & 3 deletions src/Illuminate/Foundation/Console/ClosureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Console\Command;
use Illuminate\Console\ManuallyFailedException;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Traits\ForwardsCalls;
use ReflectionFunction;
Expand Down Expand Up @@ -58,9 +59,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

return (int) $this->laravel->call(
$this->callback->bindTo($this, $this), $parameters
);
try {
return (int) $this->laravel->call(
$this->callback->bindTo($this, $this), $parameters
);
} catch (ManuallyFailedException $e) {
$this->components->error($e->getMessage());

return static::FAILURE;
}
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/Integration/Console/CommandManualFailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Illuminate\Tests\Integration\Console;

use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Command;
use Illuminate\Console\ManuallyFailedException;
use Orchestra\Testbench\TestCase;

class CommandManualFailTest extends TestCase
{
protected function setUp(): void
{
Artisan::starting(function ($artisan) {
$artisan->resolveCommands([
FailingCommandStub::class,
]);
});

parent::setUp();
}

public function testFailArtisanCommandManually()
{
$this->artisan('app:fail')->assertFailed();
}

public function testCreatesAnExceptionFromString()
{
$this->expectException(ManuallyFailedException::class);
$command = new Command;
$command->fail('Whoops!');
}

public function testCreatesAnExceptionFromNull()
{
$this->expectException(ManuallyFailedException::class);
$command = new Command;
$command->fail();
}
}

class FailingCommandStub extends Command
{
protected $signature = 'app:fail';

public function handle()
{
$this->trigger_failure();

// This should never be reached.
return static::SUCCESS;
}

protected function trigger_failure()
{
$this->fail('Whoops!');
}
}