Skip to content

Console command does not always return error code #22539

Closed
@steevanb

Description

@steevanb
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.2.7

When a Console Command throw an exception, sometimes exit code will be 1, sometimes 0

I think this is not only for 3.2, i have this problem since long time.

<?php

declare(strict_types=1);

namespace Foo;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class GenerateInvoiceCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('foo:bar');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // $toto is undefined, it will throw a ContextErrorException
        // exit code 1
        echo $toto;

        // exit code 1
        throw new \Exception('foo');
        
        // unkonwMethod() does not exist, it will throw an UndefinedMethodException
        // exit code 0
        $output->unkonwnMethod();
    }
}
php bin/console foo:bar
echo "$?"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions