Skip to content

Commit 52d372a

Browse files
committed
Add InvalidSessionAttributeException for better error clarity
1 parent 558c038 commit 52d372a

3 files changed

Lines changed: 14 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-24 - Custom Exceptions for better clarity
2+
**Learning:** Generic exceptions like `\InvalidArgumentException` can hide the root cause and make debugging harder.
3+
**Action:** Prefer creating explicitly named custom domain exceptions (e.g., `InvalidSessionAttributeException` extending `\InvalidArgumentException`) rather than throwing generic exceptions to clearly communicate the failure context and reduce cognitive load for developers.
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 Codeception\Exception;
6+
7+
use InvalidArgumentException;
8+
9+
class InvalidSessionAttributeException extends InvalidArgumentException {}

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)