Skip to content

Commit

Permalink
Use AutoMapper exceptions (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil authored Apr 15, 2024
1 parent 60ed9b3 commit 7c35b1e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- [GH#109](https://github.com/jolicode/automapper/pull/109) Use AutoMapper exceptions

## [9.0.0-beta.2] - 2024-04-02
### Added
- [GH#95](https://github.com/jolicode/automapper/pull/95) Add Api Platform integration
Expand Down
3 changes: 2 additions & 1 deletion src/Extractor/ReadAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AutoMapper\Extractor;

use AutoMapper\Exception\CompileException;
use AutoMapper\Exception\InvalidArgumentException;
use AutoMapper\MapperContext;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(
private readonly array $context = [],
) {
if (self::TYPE_METHOD === $this->type && null === $this->sourceClass) {
throw new \InvalidArgumentException('Source class must be provided when using "method" type.');
throw new InvalidArgumentException('Source class must be provided when using "method" type.');
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Generator/CreateTargetStatementsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AutoMapper\Generator;

use AutoMapper\Exception\LogicException;
use AutoMapper\Exception\MissingConstructorArgumentsException;
use AutoMapper\Generator\Shared\CachedReflectionStatementsGenerator;
use AutoMapper\Generator\Shared\DiscriminatorStatementsGenerator;
Expand Down Expand Up @@ -322,6 +323,6 @@ private function getValueAsExpr(mixed $value): Expr
return $expr->expr;
}

throw new \LogicException('Cannot extract expr from ' . var_export($value, true));
throw new LogicException('Cannot extract expr from ' . var_export($value, true));
}
}
5 changes: 3 additions & 2 deletions src/Generator/PropertyConditionsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AutoMapper\Generator;

use AutoMapper\Exception\LogicException;
use AutoMapper\MapperContext;
use AutoMapper\Metadata\GeneratorMetadata;
use AutoMapper\Metadata\PropertyMetadata;
Expand Down Expand Up @@ -258,7 +259,7 @@ private function customCondition(GeneratorMetadata $metadata, PropertyMetadata $
}

if ($argumentsCount > 2) {
throw new \LogicException('Callable condition must have 1 or 2 arguments required, but it has ' . $argumentsCount);
throw new LogicException('Callable condition must have 1 or 2 arguments required, but it has ' . $argumentsCount);
}
}

Expand Down Expand Up @@ -302,6 +303,6 @@ private function customCondition(GeneratorMetadata $metadata, PropertyMetadata $
return $expr->expr;
}

throw new \LogicException('Cannot use callback or create expression language condition from expression "' . $propertyMetadata->if . "'");
throw new LogicException('Cannot use callback or create expression language condition from expression "' . $propertyMetadata->if . "'");
}
}
3 changes: 2 additions & 1 deletion src/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AutoMapper\Loader;

use AutoMapper\Exception\RuntimeException;
use AutoMapper\Generator\MapperGenerator;
use AutoMapper\Metadata\MapperMetadata;
use AutoMapper\Metadata\MetadataFactory;
Expand Down Expand Up @@ -128,7 +129,7 @@ private function write(string $file, string $contents): void
$fp = fopen($file, 'w');

if (false === $fp) {
throw new \RuntimeException(sprintf('Could not open file "%s"', $file));
throw new RuntimeException(sprintf('Could not open file "%s"', $file));
}

if (flock($fp, LOCK_EX)) {
Expand Down
3 changes: 2 additions & 1 deletion src/MapperContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AutoMapper;

use AutoMapper\Exception\CircularReferenceException;
use AutoMapper\Exception\RuntimeException;

/**
* Context for mapping.
Expand Down Expand Up @@ -326,7 +327,7 @@ public static function getForcedTimezone(array $context): ?\DateTimeZone
try {
return new \DateTimeZone($timezone);
} catch (\Exception $e) {
throw new \RuntimeException("Invalid timezone \"$timezone\" passed to automapper context.", previous: $e);
throw new RuntimeException("Invalid timezone \"$timezone\" passed to automapper context.", previous: $e);
}
}
}
4 changes: 3 additions & 1 deletion src/Provider/ProviderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace AutoMapper\Provider;

use AutoMapper\Exception\InvalidArgumentException;

/**
* @internal
*/
Expand Down Expand Up @@ -34,7 +36,7 @@ public function __construct(iterable $providers)
public function getProvider(string $id): ProviderInterface
{
if (!\array_key_exists($id, $this->providers)) {
throw new \InvalidArgumentException(sprintf('Provider with id "%s" not found.', $id));
throw new InvalidArgumentException(sprintf('Provider with id "%s" not found.', $id));
}

return $this->providers[$id];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AutoMapper\ConstructorStrategy;
use AutoMapper\Event\PropertyMetadataEvent;
use AutoMapper\EventListener\Symfony\AdvancedNameConverterListener;
use AutoMapper\Exception\LogicException;
use AutoMapper\Loader\ClassLoaderInterface;
use AutoMapper\Loader\EvalLoader;
use AutoMapper\Loader\FileLoader;
Expand Down Expand Up @@ -94,15 +95,15 @@ public function load(array $configs, ContainerBuilder $container): void

if ($config['serializer']) {
if (!interface_exists(SerializerInterface::class)) {
throw new \LogicException('The "symfony/serializer" component is required to use the "serializer" feature.');
throw new LogicException('The "symfony/serializer" component is required to use the "serializer" feature.');
}

$loader->load('event_serializer.php');
}

if ($config['normalizer']['enabled']) {
if (!interface_exists(NormalizerInterface::class)) {
throw new \LogicException('The "symfony/serializer" component is required to use the "normalizer" feature.');
throw new LogicException('The "symfony/serializer" component is required to use the "normalizer" feature.');
}

$loader->load('normalizer.php');
Expand Down
3 changes: 2 additions & 1 deletion src/Transformer/ExpressionLanguageTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AutoMapper\Transformer;

use AutoMapper\Exception\LogicException;
use AutoMapper\Generator\UniqueVariableScope;
use AutoMapper\Metadata\PropertyMetadata;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -33,6 +34,6 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
return [$expr->expr, []];
}

throw new \LogicException('Cannot use callback or create expression language condition from expression "' . $this->expression . "'");
throw new LogicException('Cannot use callback or create expression language condition from expression "' . $this->expression . "'");
}
}
3 changes: 2 additions & 1 deletion src/Transformer/FixedValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AutoMapper\Transformer;

use AutoMapper\Exception\LogicException;
use AutoMapper\Generator\UniqueVariableScope;
use AutoMapper\Metadata\PropertyMetadata;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -33,6 +34,6 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
return [$expr->expr, []];
}

throw new \LogicException('Cannot create php code from value ' . json_encode($this->value));
throw new LogicException('Cannot create php code from value ' . json_encode($this->value));
}
}

0 comments on commit 7c35b1e

Please sign in to comment.