Skip to content

Commit 6b89cc2

Browse files
authored
Add exception code and previous exception to package exceptions (#470)
1 parent 54ee3e0 commit 6b89cc2

6 files changed

Lines changed: 51 additions & 6 deletions

src/Exception/InvalidCallbackReturnTypeException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function __construct(
2222
* @var mixed The actual wrong value returned from a callback.
2323
*/
2424
mixed $returnValue,
25+
/**
26+
* @var int The Exception code.
27+
*/
28+
int $code = 0,
2529
/**
2630
* @var Throwable|null The previous throwable used for the exception chaining.
2731
*/
@@ -33,7 +37,7 @@ public function __construct(
3337
get_debug_type($returnValue),
3438
);
3539

36-
parent::__construct($message, 0, $previous);
40+
parent::__construct($message, $code, $previous);
3741
}
3842

3943
/**

src/Exception/RuleHandlerInterfaceNotImplementedException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function __construct(
2121
* @param mixed A variable retrieved from the container.
2222
*/
2323
mixed $value,
24+
/**
25+
* @var int The Exception code.
26+
*/
27+
int $code = 0,
2428
/**
2529
* @var Throwable|null The previous throwable used for the exception chaining.
2630
*/
@@ -36,7 +40,7 @@ class_exists($type)
3640
$type,
3741
RuleHandlerInterface::class,
3842
),
39-
0,
43+
$code,
4044
$previous
4145
);
4246
}

src/Exception/RuleHandlerNotFoundException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function __construct(
1818
* @var string A class name from failed attempt of search in the container.
1919
*/
2020
string $className,
21+
/**
22+
* @var int The Exception code.
23+
*/
24+
int $code = 0,
2125
/**
2226
* @var Throwable|null The previous throwable used for the exception chaining.
2327
*/
@@ -29,7 +33,7 @@ public function __construct(
2933
$className,
3034
$className,
3135
),
32-
0,
36+
$code,
3337
$previous,
3438
);
3539
}

src/Exception/UnexpectedRuleException.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Validator\Exception;
66

77
use InvalidArgumentException;
8+
use Throwable;
89

910
/**
1011
* An exception used by rule handlers to guarantee that passed rule have desired type. Every handler's validation code
@@ -42,14 +43,24 @@ public function __construct(
4243
/**
4344
* @var object An actual given object that's not an instance of `$expectedClassName`.
4445
*/
45-
object $actualObject
46+
object $actualObject,
47+
/**
48+
* @var int The Exception code.
49+
*/
50+
int $code = 0,
51+
/**
52+
* @var Throwable|null The previous throwable used for the exception chaining.
53+
*/
54+
?Throwable $previous = null,
4655
) {
4756
parent::__construct(
4857
sprintf(
4958
'Expected "%s", but "%s" given.',
5059
$expectedClassName,
5160
$actualObject::class
52-
)
61+
),
62+
$code,
63+
$previous,
5364
);
5465
}
5566
}

src/RuleHandlerResolver/RuleHandlerContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function resolve(string $className): RuleHandlerInterface
2222
try {
2323
$ruleHandler = $this->container->get($className);
2424
} catch (NotFoundExceptionInterface $e) {
25-
throw new RuleHandlerNotFoundException($className, $e);
25+
throw new RuleHandlerNotFoundException($className, previous: $e);
2626
}
2727

2828
if (!$ruleHandler instanceof RuleHandlerInterface) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Exception;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use stdClass;
9+
use Yiisoft\Validator\Exception\UnexpectedRuleException;
10+
use Yiisoft\Validator\Rule\Number;
11+
12+
final class UnexpectedRuleExceptionTest extends TestCase
13+
{
14+
public function testBase(): void
15+
{
16+
$exception = new UnexpectedRuleException(Number::class, new stdClass());
17+
18+
$this->assertSame('Expected "Yiisoft\Validator\Rule\Number", but "stdClass" given.', $exception->getMessage());
19+
$this->assertSame(0, $exception->getCode());
20+
$this->assertNull($exception->getPrevious());
21+
}
22+
}

0 commit comments

Comments
 (0)