TaskerBridges/src/ExitCodeTranslator.php

38 lines
830 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Phred\TaskerBridges;
use Phred\ConsoleContracts\ExitCode;
use Throwable;
use InvalidArgumentException;
use RuntimeException;
use LogicException;
/**
* Translates exceptions and codes into Phred standardized exit codes.
*/
final class ExitCodeTranslator
{
/**
* Translates an exception to an exit code.
*
* @param Throwable $throwable
* @return int
*/
public static function translate(Throwable $throwable): int
{
if ($throwable instanceof InvalidArgumentException) {
return ExitCode::USAGE;
}
if ($throwable instanceof RuntimeException || $throwable instanceof LogicException) {
return ExitCode::SOFTWARE;
}
// Default to generic failure
return ExitCode::FAILURE;
}
}