Skip to content
Snippets Groups Projects
Commit bf9f7218 authored by Jan Kuchař's avatar Jan Kuchař
Browse files

print suppressed exceptions & their parents information into exception...

print suppressed exceptions & their parents information into exception message, as this is supported by all debugging tools
parent ca7517d5
Branches
Tags
1 merge request!1print suppressed exceptions & their parents information into message
Pipeline #15612 passed
...@@ -14,6 +14,7 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */ ...@@ -14,6 +14,7 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */
public function addSuppressed(\Throwable ...$exceptions): void public function addSuppressed(\Throwable ...$exceptions): void
{ {
foreach($exceptions as $exception) { foreach($exceptions as $exception) {
$this->addTextVersionOfExceptionToMessage($exception);
$this->suppressedExceptions[] = $exception; $this->suppressedExceptions[] = $exception;
} }
} }
...@@ -23,4 +24,43 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */ ...@@ -23,4 +24,43 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */
return $this->suppressedExceptions; return $this->suppressedExceptions;
} }
} private function addTextVersionOfExceptionToMessage(\Throwable $throwable): void
\ No newline at end of file {
if ($this->suppressedExceptions === []) {
$this->message .= "\nSuppressed exceptions:\n";
}
$moveRight = function(string $textToMoveRight, int $offset): string {
$replaceWith = "\n" . \str_repeat(' ', $offset);
return str_replace(
["\r\n","\n","\r"],
$replaceWith,
$textToMoveRight
);
};
$renderSingle = function(\Throwable $throwable): string {
$message = $throwable->getMessage();
$type = \get_class($throwable);
$message =
$message === ''
? $type
: "{$message} ({$type})";
$fileRelativePath = str_replace(\getcwd() . DIRECTORY_SEPARATOR, '', $throwable->getFile());
return "{$fileRelativePath}:{$throwable->getLine()} - {$message}";
};
$renderTree = function(\Throwable $throwable) use ($moveRight, $renderSingle): string {
$string = $renderSingle($throwable);
$previous = $throwable;
while (($previous = $previous->getPrevious()) !== NULL) {
$string .= "\n previous: {$moveRight($renderSingle($previous), 12)}";
}
return $string;
};
$this->message .= "- {$moveRight($renderTree($throwable), 2)}\n";
}
}
<?php declare(strict_types=1);
namespace Grifart\SuppressedExceptions\__tests;
use Grifart\SuppressedExceptions\SuppressedExceptions;
use Grifart\SuppressedExceptions\WithSuppressedExceptions;
use Symfony\Component\Console\Exception\RuntimeException;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class TestingSuppressedExceptionsException extends \RuntimeException implements WithSuppressedExceptions
{
use SuppressedExceptions;
}
$previous = new TestingSuppressedExceptionsException('previous', -1, new RuntimeException());
$exception1 = new TestingSuppressedExceptionsException('message', 42, $previous);
$exception1->addSuppressed(new RuntimeException('message', 0, $previous));
$exception2 = new TestingSuppressedExceptionsException('message2', 42);
$exception2->addSuppressed($previous);
$exception2->addSuppressed($exception1);
Assert::exception(
function() use ($exception2) {
throw $exception2;
},
TestingSuppressedExceptionsException::class,
\file_get_contents(__DIR__ . '/nested_exception-message.txt')
);
message2
Suppressed exceptions:
- nested.phpt:18 - previous (Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException)
previous: nested.phpt:18 - Symfony\Component\Console\Exception\RuntimeException
- nested.phpt:19 - message
Suppressed exceptions:
- nested.phpt:20 - message (Symfony\Component\Console\Exception\RuntimeException)
previous: nested.phpt:18 - previous (Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException)
previous: nested.phpt:18 - Symfony\Component\Console\Exception\RuntimeException
(Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException)
previous: nested.phpt:18 - previous (Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException)
previous: nested.phpt:18 - Symfony\Component\Console\Exception\RuntimeException
...@@ -19,15 +19,19 @@ $previous = new TestingSuppressedExceptionsException('previous', -1, new Runtime ...@@ -19,15 +19,19 @@ $previous = new TestingSuppressedExceptionsException('previous', -1, new Runtime
$exception = new TestingSuppressedExceptionsException('message', 42, $previous); $exception = new TestingSuppressedExceptionsException('message', 42, $previous);
$exception->addSuppressed($suppressed1 = new \RuntimeException()); $exception->addSuppressed($suppressed1 = new \RuntimeException());
$exception->addSuppressed($suppressed2 = new \LogicException()); $exception->addSuppressed($suppressed2 = new \LogicException('This is message'));
$exception->addSuppressed($suppressed3 = new \Error()); $exception->addSuppressed($suppressed3 = new \Error());
$exception->addSuppressed($suppressed4 = new \Exception()); $exception->addSuppressed($suppressed4 = new \Exception('With previous', 0, $previous));
$exception->addSuppressed($suppressed5 = new TestingSuppressedExceptionsException()); $exception->addSuppressed($suppressed5 = new TestingSuppressedExceptionsException());
// test that can be thrown // test that can be thrown
Assert::exception(function() use ($exception) { Assert::exception(
throw $exception; function() use ($exception) {
}, TestingSuppressedExceptionsException::class); throw $exception;
},
TestingSuppressedExceptionsException::class,
\file_get_contents(__DIR__ . '/simple_exception-message.txt')
);
// previous // previous
Assert::same($previous, $exception->getPrevious()); Assert::same($previous, $exception->getPrevious());
......
message
Suppressed exceptions:
- simple.phpt:21 - RuntimeException
- simple.phpt:22 - This is message (LogicException)
- simple.phpt:23 - Error
- simple.phpt:24 - With previous (Exception)
previous: simple.phpt:18 - previous (Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException)
previous: simple.phpt:18 - Symfony\Component\Console\Exception\RuntimeException
- simple.phpt:25 - Grifart\SuppressedExceptions\__tests\TestingSuppressedExceptionsException
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment