diff --git a/src/Converter.php b/src/Converter.php index e1783bf..ac480db 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -2,12 +2,14 @@ namespace Vesper\UnitConversion; +use Vesper\UnitConversion\Exceptions\CannotConvertUnitException; + class Converter { public function convert(Unit $from, Unit $to, float $value = 1): float { if (!$from->canConvertTo($to)) { - throw new \Exception("Cannot convert from [$from] to [$to]"); + throw new CannotConvertUnitException("Cannot convert from [$from] to [$to]"); } $fromOffset = $from->getPart(0)->getRatio() * $from->getPart(0)->getOffset(); diff --git a/src/Exceptions/CannotConvertUnitException.php b/src/Exceptions/CannotConvertUnitException.php new file mode 100644 index 0000000..07a859e --- /dev/null +++ b/src/Exceptions/CannotConvertUnitException.php @@ -0,0 +1,9 @@ +registry->get($token['value']); if ($unit === null) { - throw new \Exception("Unknown unit: {$token['value']}"); + throw new InvalidUnitException("Unknown unit: {$token['value']}"); } $powerSign = $prevToken && $prevToken['type'] === 'operator' && $prevToken['value'] === '/' diff --git a/src/Registry.php b/src/Registry.php index 0262744..df3ddc1 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -2,6 +2,8 @@ namespace Vesper\UnitConversion; +use Vesper\UnitConversion\Exceptions\InvalidUnitException; + class Registry { protected array $registry = []; @@ -23,12 +25,12 @@ public function alias(string $name, array|string $aliases): self $base = $this->get($name); if ($base === null) { - throw new \Exception("Cannot alias unknown unit [$name]"); + throw new InvalidUnitException("Cannot alias unknown unit [$name]"); } foreach ((array)$aliases as $alias) { if (isset($this->registry[$alias])) { - throw new \Exception("Name [$alias] is already registered"); + throw new InvalidUnitException("Name [$alias] is already registered"); } $this->registry[$alias] = $base; diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index 57ef102..0c8a384 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -5,6 +5,7 @@ use Vesper\UnitConversion\Dimension; use Vesper\UnitConversion\Unit; use Vesper\UnitConversion\UnitPart; +use Vesper\UnitConversion\Exceptions\CannotConvertUnitException; final class ConverterTest extends TestCase { @@ -27,7 +28,7 @@ public function test_converts_incompatible_throws() new UnitPart(1, Dimension::LENGTH, 1), ); - $this->expectException(\Exception::class); + $this->expectException(CannotConvertUnitException::class); $this->expectExceptionMessage("Cannot convert from [$m] to [$m2]"); $this->converter->convert($m, $m2); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 79359cc..ad31e1e 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -5,6 +5,7 @@ use Vesper\UnitConversion\Parser; use Vesper\UnitConversion\Registry; use Vesper\UnitConversion\RegistryBuilder; +use Vesper\UnitConversion\Exceptions\InvalidUnitException; final class ParserTest extends TestCase { @@ -169,7 +170,7 @@ public function test_ignores_whitespace() public function test_throws_on_unknown_token() { - $this->expectException(\Exception::class); + $this->expectException(InvalidUnitException::class); $this->expectExceptionMessage('Unknown unit: yeet'); $this->parser->parse('kilometer / yeet'); diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index 1aaa6b8..8e69cb5 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -5,6 +5,7 @@ use Vesper\UnitConversion\Registry; use Vesper\UnitConversion\Unit; use Vesper\UnitConversion\UnitPart; +use Vesper\UnitConversion\Exceptions\InvalidUnitException; final class RegistryTest extends TestCase { @@ -42,7 +43,7 @@ public function test_alias_unknown_throws() { $this->assertNull($this->registry->get('yeet')); - $this->expectException(Exception::class); + $this->expectException(InvalidUnitException::class); $this->expectExceptionMessage('Cannot alias unknown unit [yeet]'); $this->registry->alias('yeet', 'y'); @@ -50,7 +51,7 @@ public function test_alias_unknown_throws() public function test_alias_overwrite_throws() { - $this->expectException(Exception::class); + $this->expectException(InvalidUnitException::class); $this->expectExceptionMessage('Name [gram] is already registered'); $this->registry->alias('gram', 'gram');