Skip to content

Commit 10ed913

Browse files
authored
feat(exceptions): better exception naming, add interface to group all exceptions (#118)
This implement the following pattern : * All exceptions throw by the lib implement the `AutoMapperException` interface * Split exceptions in 3 categories : * MetadataException : happens when loading metadata * CompileException : happens when generating code * RuntimeException : happens when executing the mapping
2 parents 1b23ffc + aa758bf commit 10ed913

20 files changed

+42
-35
lines changed

src/AutoMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AutoMapper;
66

7-
use AutoMapper\Exception\NoMappingFoundException;
7+
use AutoMapper\Exception\InvalidMappingException;
88
use AutoMapper\Generator\MapperGenerator;
99
use AutoMapper\Generator\Shared\ClassDiscriminatorResolver;
1010
use AutoMapper\Loader\ClassLoaderInterface;
@@ -109,7 +109,7 @@ public function map(array|object $source, string|array|object $target, array $co
109109
}
110110

111111
if ('array' === $sourceType && 'array' === $targetType) {
112-
throw new NoMappingFoundException('Cannot map this value, both source and target are array.');
112+
throw new InvalidMappingException('Cannot map this value, both source and target are array.');
113113
}
114114

115115
return $this->getMapper($sourceType, $targetType)->map($source, $context);

src/Exception/NoMappingFoundException.php renamed to src/Exception/AutoMapperException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Joel Wurtz <[email protected]>
99
*/
10-
final class NoMappingFoundException extends RuntimeException
10+
interface AutoMapperException
1111
{
1212
}

src/Exception/BadMapDefinitionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Joel Wurtz <[email protected]>
99
*/
10-
final class BadMapDefinitionException extends RuntimeException
10+
final class BadMapDefinitionException extends MetadataException
1111
{
1212
}

src/Exception/CompileException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Joel Wurtz <[email protected]>
99
*/
10-
final class CompileException extends RuntimeException
10+
final class CompileException extends \LogicException implements AutoMapperException
1111
{
1212
}

src/Exception/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Baptiste Leduc <[email protected]>
99
*/
10-
final class InvalidArgumentException extends \InvalidArgumentException
10+
final class InvalidArgumentException extends \InvalidArgumentException implements AutoMapperException
1111
{
1212
}

src/Exception/InvalidMappingException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Joel Wurtz <[email protected]>
99
*/
10-
final class InvalidMappingException extends RuntimeException
10+
final class InvalidMappingException extends MetadataException
1111
{
1212
}

src/Exception/LogicException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
/**
88
* @author Baptiste Leduc <[email protected]>
99
*/
10-
final class LogicException extends \LogicException
10+
final class LogicException extends \LogicException implements AutoMapperException
1111
{
1212
}

src/Exception/MetadataException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AutoMapper\Exception;
6+
7+
class MetadataException extends \RuntimeException implements AutoMapperException
8+
{
9+
}

src/Exception/MissingConstructorArgumentsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AutoMapper\Exception;
66

7-
final class MissingConstructorArgumentsException extends \RuntimeException
7+
final class MissingConstructorArgumentsException extends RuntimeException
88
{
99
/**
1010
* @param string[] $missingArguments

src/Exception/RuntimeException.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace AutoMapper\Exception;
66

7-
/**
8-
* @author Joel Wurtz <[email protected]>
9-
*/
10-
class RuntimeException extends \RuntimeException
7+
class RuntimeException extends \RuntimeException implements AutoMapperException
118
{
129
}

0 commit comments

Comments
 (0)