Skip to content

Commit c437554

Browse files
authored
chore: improve exceptions (#8)
1 parent d2758bb commit c437554

File tree

66 files changed

+429
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+429
-271
lines changed

Attribute/Formatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Symfony\Component\Marshaller\Attribute;
1111

12+
use Symfony\Component\Marshaller\Exception\InvalidArgumentException;
13+
1214
/**
1315
* @author Mathias Arlaud <[email protected]>
1416
*/
@@ -28,13 +30,13 @@ public function __construct(
2830
) {
2931
if (null !== $marshal) {
3032
if (!\is_callable($marshal)) {
31-
throw new \InvalidArgumentException(sprintf('Parameter "$marshal" of attribute "%s" must be a valid callable.', self::class));
33+
throw new InvalidArgumentException(sprintf('Parameter "$marshal" of attribute "%s" must be a valid callable.', self::class));
3234
}
3335
}
3436

3537
if (null !== $unmarshal) {
3638
if (!\is_callable($unmarshal)) {
37-
throw new \InvalidArgumentException(sprintf('Parameter "$unmarshal" of attribute "%s" must be a valid callable.', self::class));
39+
throw new InvalidArgumentException(sprintf('Parameter "$unmarshal" of attribute "%s" must be a valid callable.', self::class));
3840
}
3941
}
4042

Context/Option/HookOption.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Symfony\Component\Marshaller\Context\Option;
1111

12+
use Symfony\Component\Marshaller\Exception\InvalidArgumentException;
13+
1214
/**
1315
* @author Mathias Arlaud <[email protected]>
1416
*/
@@ -28,7 +30,7 @@ public function __construct(array $hooks)
2830

2931
foreach ($hooks as $hookName => $hook) {
3032
if (!\is_callable($hook)) {
31-
throw new \InvalidArgumentException(sprintf('Hook "%s" of attribute "%s" is an invalid callable.', $hookName, self::class));
33+
throw new InvalidArgumentException(sprintf('Hook "%s" of attribute "%s" is an invalid callable.', $hookName, self::class));
3234
}
3335

3436
$closures[$hookName] = \Closure::fromCallable($hook);

Context/Option/TypeFormatterOption.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Symfony\Component\Marshaller\Context\Option;
1111

12+
use Symfony\Component\Marshaller\Exception\InvalidArgumentException;
13+
1214
/**
1315
* @author Mathias Arlaud <[email protected]>
1416
*/
@@ -28,7 +30,7 @@ public function __construct(array $typeFormatters)
2830

2931
foreach ($typeFormatters as $typeName => $formatter) {
3032
if (!\is_callable($formatter)) {
31-
throw new \InvalidArgumentException(sprintf('Formatter "%s" of attribute "%s" is an invalid callable.', $typeName, self::class));
33+
throw new InvalidArgumentException(sprintf('Formatter "%s" of attribute "%s" is an invalid callable.', $typeName, self::class));
3234
}
3335

3436
$formatters[$typeName] = \Closure::fromCallable($formatter);

Internal/Exception/InvalidJsonException.php renamed to Exception/CircularReferenceException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfony\Component\Marshaller\Internal\Exception;
10+
namespace Symfony\Component\Marshaller\Exception;
1111

1212
/**
1313
* @author Mathias Arlaud <[email protected]>
14-
*
15-
* @internal
1614
*/
17-
final class InvalidJsonException extends InvalidResourceException
15+
final class CircularReferenceException extends UnexpectedValueException
1816
{
1917
}

Exception/ExceptionInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
interface ExceptionInterface extends \Throwable
16+
{
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
16+
{
17+
}

Internal/Exception/InvalidResourceException.php renamed to Exception/InvalidResourceException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfony\Component\Marshaller\Internal\Exception;
10+
namespace Symfony\Component\Marshaller\Exception;
1111

1212
/**
1313
* @author Mathias Arlaud <[email protected]>
14-
*
15-
* @internal
1614
*/
17-
abstract class InvalidResourceException extends \InvalidArgumentException
15+
final class InvalidResourceException extends UnexpectedValueException
1816
{
1917
}

Exception/InvalidTypeException.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
final class InvalidTypeException extends InvalidArgumentException
16+
{
17+
public function __construct(string $type, string $cause = null)
18+
{
19+
$message = sprintf('Invalid "%s" type', $type);
20+
21+
if (null !== $cause) {
22+
$message .= ': '.$cause;
23+
}
24+
25+
parent::__construct($message.'.');
26+
}
27+
}

Exception/LogicException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
class LogicException extends \LogicException implements ExceptionInterface
16+
{
17+
}

Exception/MissingTypeException.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
final class MissingTypeException extends InvalidArgumentException
16+
{
17+
public function __construct(string $path, string $type)
18+
{
19+
parent::__construct(sprintf('Type of "%s" %s has not been defined.', $path, $type));
20+
}
21+
22+
public static function createForProperty(\ReflectionProperty $property): self
23+
{
24+
return new self(sprintf('%s::$%s', $property->getDeclaringClass()->getName(), $property->getName()), 'property');
25+
}
26+
27+
// TODO ReturnType -> FunctionReturn
28+
29+
public static function createForFunctionReturn(\ReflectionFunctionAbstract $function): self
30+
{
31+
/** @var \ReflectionClass<object>|null $declaringClass */
32+
$declaringClass = $function instanceof \ReflectionMethod ? $function->getDeclaringClass() : $function->getClosureScopeClass();
33+
34+
$path = null !== $declaringClass
35+
? sprintf('%s::%s()', $declaringClass->getName(), $function->getName())
36+
: sprintf('%s()', $function->getName());
37+
38+
return new self($path, 'return');
39+
}
40+
41+
public static function createForFunctionParameter(\ReflectionParameter $parameter): self
42+
{
43+
$function = $parameter->getDeclaringFunction();
44+
45+
/** @var \ReflectionClass<object>|null $declaringClass */
46+
$declaringClass = $function instanceof \ReflectionMethod ? $function->getDeclaringClass() : $function->getClosureScopeClass();
47+
48+
$path = null !== $declaringClass
49+
? sprintf('%s::%s($%s)', $declaringClass->getName(), $function->getName(), $parameter->getName())
50+
: sprintf('%s($%s)', $function->getName(), $parameter->getName());
51+
52+
return new self($path, 'property');
53+
}
54+
}

Exception/RuntimeException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
class RuntimeException extends \RuntimeException implements ExceptionInterface
16+
{
17+
}

Internal/Exception/InvalidTokenException.php renamed to Exception/UnexpectedTokenException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfony\Component\Marshaller\Internal\Exception;
10+
namespace Symfony\Component\Marshaller\Exception;
1111

1212
/**
1313
* @author Mathias Arlaud <[email protected]>
14-
*
15-
* @internal
1614
*/
17-
final class InvalidTokenException extends \InvalidArgumentException
15+
final class UnexpectedTokenException extends UnexpectedValueException
1816
{
1917
public function __construct(string $expected, string $actual)
2018
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
class UnexpectedValueException extends \UnexpectedValueException implements ExceptionInterface
16+
{
17+
}

Internal/Exception/UnknownFormatException.php renamed to Exception/UnsupportedFormatException.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfony\Component\Marshaller\Internal\Exception;
10+
namespace Symfony\Component\Marshaller\Exception;
1111

1212
/**
1313
* @author Mathias Arlaud <[email protected]>
14-
*
15-
* @internal
1614
*/
17-
final class UnknownFormatException extends \InvalidArgumentException
15+
final class UnsupportedFormatException extends InvalidArgumentException
1816
{
1917
public function __construct(string $format)
2018
{
21-
parent::__construct(sprintf('Unknown "%s" format.', $format));
19+
parent::__construct(sprintf('"%s" format is not supported.', $format));
2220
}
2321
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\Component\Marshaller\Exception;
11+
12+
/**
13+
* @author Mathias Arlaud <[email protected]>
14+
*/
15+
final class UnsupportedTypeException extends InvalidArgumentException
16+
{
17+
public function __construct(string $type)
18+
{
19+
parent::__construct(sprintf('"%s" type is not supported.', $type));
20+
}
21+
}

Hook/Marshal/ObjectHook.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Symfony\Component\Marshaller\Hook\Marshal;
1111

12+
use Symfony\Component\Marshaller\Exception\InvalidArgumentException;
13+
use Symfony\Component\Marshaller\Exception\InvalidTypeException;
1214
use Symfony\Component\Marshaller\Type\TypeExtractorInterface;
1315

1416
/**
@@ -76,7 +78,7 @@ private function addGenericParameterTypes(string $type, array $context): array
7678
$genericParameters[] = $currentGenericParameter;
7779

7880
if (0 !== $nestedLevel) {
79-
throw new \InvalidArgumentException(sprintf('Invalid "%s" type.', $type));
81+
throw new InvalidTypeException($type);
8082
}
8183

8284
if (!class_exists($genericType)) {
@@ -86,7 +88,7 @@ private function addGenericParameterTypes(string $type, array $context): array
8688
$templates = $this->typeExtractor->extractTemplateFromClass(new \ReflectionClass($genericType));
8789

8890
if (\count($templates) !== \count($genericParameters)) {
89-
throw new \InvalidArgumentException(sprintf('Given %d generic parameters in "%s", but %d templates are defined in "%s".', \count($genericParameters), $type, \count($templates), $genericType));
91+
throw new InvalidArgumentException(sprintf('Given %d generic parameters in "%s", but %d templates are defined in "%s".', \count($genericParameters), $type, \count($templates), $genericType));
9092
}
9193

9294
foreach ($genericParameters as $i => $genericParameter) {

Hook/Marshal/PropertyHook.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Symfony\Component\Marshaller\Hook\Marshal;
1111

12+
use Symfony\Component\Marshaller\Exception\InvalidArgumentException;
1213
use Symfony\Component\Marshaller\Type\TypeExtractorInterface;
1314

1415
/**
@@ -80,18 +81,18 @@ private function accessor(string $propertyIdentifier, ?\ReflectionFunction $prop
8081
}
8182

8283
if (!$propertyFormatter->getClosureScopeClass()?->hasMethod($propertyFormatter->getName()) || !$propertyFormatter->isStatic()) {
83-
throw new \InvalidArgumentException(sprintf('Property formatter "%s" must be a static method.', $propertyIdentifier));
84+
throw new InvalidArgumentException(sprintf('Property formatter "%s" must be a static method.', $propertyIdentifier));
8485
}
8586

8687
if (($returnType = $propertyFormatter->getReturnType()) instanceof \ReflectionNamedType && ('void' === $returnType->getName() || 'never' === $returnType->getName())) {
87-
throw new \InvalidArgumentException(sprintf('Return type of property formatter "%s" must not be "void" nor "never".', $propertyIdentifier));
88+
throw new InvalidArgumentException(sprintf('Return type of property formatter "%s" must not be "void" nor "never".', $propertyIdentifier));
8889
}
8990

9091
if (null !== ($contextParameter = $propertyFormatter->getParameters()[1] ?? null)) {
9192
$contextParameterType = $contextParameter->getType();
9293

9394
if (!$contextParameterType instanceof \ReflectionNamedType || 'array' !== $contextParameterType->getName()) {
94-
throw new \InvalidArgumentException(sprintf('Second argument of property formatter "%s" must be an array.', $propertyIdentifier));
95+
throw new InvalidArgumentException(sprintf('Second argument of property formatter "%s" must be an array.', $propertyIdentifier));
9596
}
9697
}
9798

0 commit comments

Comments
 (0)