Skip to content

Commit

Permalink
fix: Make the invalid diff mode error message understandable
Browse files Browse the repository at this point in the history
Previously this was resulting in a FatalError with an obscure message.
  • Loading branch information
theofidry committed Oct 15, 2023
1 parent 7e774df commit 5003fc9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5003fc9

Please sign in to comment.