From 5003fc9054e39765f797fad63297c874d0759a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 15 Oct 2023 16:30:44 +0200 Subject: [PATCH] fix: Make the invalid diff mode error message understandable Previously this was resulting in a FatalError with an obscure message. --- src/Console/Command/Diff.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Console/Command/Diff.php b/src/Console/Command/Diff.php index 192de1854..488873c31 100644 --- a/src/Console/Command/Diff.php +++ b/src/Console/Command/Diff.php @@ -24,10 +24,12 @@ use KevinGH\Box\Phar\PharInfo; use SebastianBergmann\Diff\Differ; use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; +use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Filesystem\Path; +use ValueError; use Webmozart\Assert\Assert; use function array_map; use function explode; @@ -268,7 +270,23 @@ private static function getDiffMode(IO $io): DiffMode return DiffMode::FILE_NAME; } - return DiffMode::from($io->getOption(self::DIFF_OPTION)->asNonEmptyString()); + $rawDiffOption = $io->getOption(self::DIFF_OPTION)->asNonEmptyString(); + + try { + return DiffMode::from($rawDiffOption); + } catch (ValueError) { + // Rethrow a more user-friendly error message + throw new RuntimeException( + sprintf( + 'Invalid diff mode "%s". Expected one of: "%s".', + $rawDiffOption, + implode( + '", "', + DiffMode::values(), + ), + ), + ); + } } private static function getChecksumAlgorithm(IO $io): string