Exit codes of a Symfony command: SUCCESS, FAILURE, INVALID

The exit codes of a Symfony command tell the script or the CI that runs it whether it succeeded: 0 on success, anything else otherwise.

if ('' === $categorie) {
    $io->error('Specify the category: --categorie "Main courses".');

    return Command::INVALID; // 2: the command is misused
}
if (null === $this->categories->findOneBy(['nom' => $categorie])) {
    $io->error(sprintf('The category "%s" does not exist.', $categorie));

    return Command::FAILURE; // 1: it couldn't do its job
}
// …
return Command::SUCCESS; // 0

__invoke() returns an integer, which Symfony turns into the process's exit code: Command::SUCCESS (0), Command::FAILURE (1) or Command::INVALID (2). A human reads SymfonyStyle's messages; a script or the CI only looks at that code, and at what changed in the database. $io->error() shows a red banner but doesn't touch the code: you also need the return. The trap: returning SUCCESS after rejecting a piece of data, on the grounds that the command "did check". The script that runs next believes the addition succeeded and carries on.

The exercises have you make Gorm's inventory fail on an unknown category; then, in commands "made more careful", find out why a nightly script no longer adds any dish without reporting an error, believes a rejected dish was added, and why a big clean-up reports deletions it didn't make.

2 exercises work on this concept, by writing code in the browser.

In the tracks

Related concepts

Console command#[Option]CommandTesterDebuggingBatch processing