Skip to content

Commit 596f378

Browse files
committed
Improving error message
1 parent b3a295a commit 596f378

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Rules/Exceptions/MustRethrowRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public function getNodeType(): string
3535
public function processNode(Node $node, Scope $scope): array
3636
{
3737
// Let's only apply the filter to \Exception, \RuntimeException or \Throwable
38-
$elected = false;
38+
$exceptionType = null;
3939
foreach ($node->types as $type) {
4040
if (in_array((string)$type, [Exception::class, RuntimeException::class, Throwable::class], true)) {
41-
$elected = true;
41+
$exceptionType = (string)$type;
4242
break;
4343
}
4444
}
4545

46-
if (!$elected) {
46+
if ($exceptionType === null) {
4747
return [];
4848
}
4949

@@ -76,7 +76,7 @@ public function isThrowFound(): bool
7676
$errors = [];
7777

7878
if (!$visitor->isThrowFound()) {
79-
$errors[] = sprintf('%scaught \Exception, \Throwable or \RuntimeException must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" to propagate the exception.', PrefixGenerator::generatePrefix($scope));
79+
$errors[] = sprintf('%scaught "%s" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception.', PrefixGenerator::generatePrefix($scope), $exceptionType);
8080
}
8181

8282
return $errors;

tests/Rules/Exceptions/MustRethrowRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public function testCheckCatchedException()
1717
{
1818
$this->analyse([__DIR__ . '/data/must_rethrow.php'], [
1919
[
20-
'caught \Exception, \Throwable or \RuntimeException must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" to propagate the exception.',
20+
'caught "Exception" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception.',
2121
18,
2222
],
2323
[
24-
'caught \Exception, \Throwable or \RuntimeException must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" to propagate the exception.',
24+
'caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception.',
2525
24,
2626
],
2727
[
28-
'In function "TestCatch\foo", caught \Exception, \Throwable or \RuntimeException must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" to propagate the exception.',
28+
'In function "TestCatch\foo", caught "RuntimeException" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception.',
2929
31,
3030
],
3131
]);

0 commit comments

Comments
 (0)