how to Abort/exit/stop/terminate in laravel console command using code
Asked Answered
M

5

12

Let's say I'm coding a command. How would I stop it completely in the middle of it running?

Example:

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        $this->exit();
    }

    // continue command stuff
}

I have tried:

throw new RuntimeException('Bad times');

But that dumps a bunch of ugliness in the terminal.

Motherland answered 30/11, 2018 at 11:54 Comment(0)
A
19

Just use a return statement instead of throwing an exception. Like...

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        // $this->exit();
        // throw new RuntimeException('Bad times');
        return self::FAILURE;
    }

    // ...
    return self::SUCCESS;
}
Antennule answered 2/3, 2019 at 13:5 Comment(1)
Notice that since Laravel 7 console commands must not return void, but only integers. In the example a return true should be changed to return 0. But stil, only returning something (0 or 1) would do the trick.Cajun
C
7

Ran into this problem as well.

Just create an exception that implements ExceptionInterface.

use Exception;
use Symfony\Component\Console\Exception\ExceptionInterface;

class ConsoleException extends Exception implements ExceptionInterface
{

}

now when you throw the error:

throw new ConsoleException('Bad times');

You get the error without the stack trace.

Cameliacamella answered 8/6, 2020 at 20:50 Comment(1)
Or just use one of the existing Symfony exceptions that implements this interface. InvalidOptionException, InvalidOptionException, MissingInputException, etc. All in the same namespace as the interface. Note there is still an "ugly" one-line error message generated, in addition to the red error box. just slightly less uglyKonstantin
C
0
private function foo()
{
    if (/* exception trigger condition */) {
        throw new \Exception('Exception message');
    }
    // ...
}

public function handle()
{
    try{
        $this->foo();
    } catch (\Exception $error){
        $this->error('An error occurred: ' . $error->getMessage());
    
        return;
    }
    
    $this->comment('All done');
}
Cyte answered 18/4, 2021 at 10:11 Comment(1)
Code only answers are discouraged. Please provide a summary of how your answer solves the problem and why it may be preferable to the other answers already provided.Gothic
A
0

Use return with the exit code number:

public function handle()
{
    $this->error('Invalid parameter: thiswillfail');
    return 1;
}

Return a 0 if all is ok, a non-zero positive value on errors:

$ ./artisan mycommand thiswillfail; echo $?
Invalid parameter: thiswillfail

1

This works with at least Laravel 6 onwards.

Aldas answered 27/8, 2021 at 13:19 Comment(0)
A
-1

You can use return or exit if you want to stop the command from within a function.

public function handle()
{
    return;
}
protected function stop() {
    exit;
}
    
public function handle()
{
    $this->stop();
}
Amanuensis answered 3/10, 2022 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.