Skip to content

Commit 134f57f

Browse files
committed
Create custom InvalidSessionAttributeException
1 parent 558c038 commit 134f57f

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

.jules/palette_dx.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 2024-05-18 - Improve Error Clarity with Custom Exceptions
2+
**Learning:** The `SessionAssertionsTrait::seeSessionHasValues` method throws a generic `InvalidArgumentException` when an invalid attribute name type is provided. Creating a custom domain exception like `InvalidSessionAttributeException` improves clarity in testing environments, clearly communicating the failure context.
3+
**Action:** Create a custom exception in `src/Codeception/Exception/` and use it instead of generic exceptions to reduce cognitive load.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Exception;
6+
7+
use InvalidArgumentException;
8+
9+
class InvalidSessionAttributeException extends InvalidArgumentException
10+
{
11+
}

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

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

55
namespace Codeception\Module\Symfony;
66

7-
use InvalidArgumentException;
7+
use Codeception\Exception\InvalidSessionAttributeException;
88
use Symfony\Component\BrowserKit\Cookie;
99
use Symfony\Component\HttpFoundation\Session\SessionFactoryInterface;
1010
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -174,7 +174,7 @@ public function seeSessionHasValues(array $bindings): void
174174
continue;
175175
}
176176
if (!is_string($expectedAttr)) {
177-
throw new InvalidArgumentException(sprintf('Attribute name must be string, %s given.', get_debug_type($expectedAttr)));
177+
throw new InvalidSessionAttributeException(sprintf('Attribute name must be string, %s given.', get_debug_type($expectedAttr)));
178178
}
179179
$this->assertTrue($session->has($expectedAttr), "No session attribute with name '{$expectedAttr}'");
180180
}

0 commit comments

Comments
 (0)