...
 
Commits (2)
......@@ -6,7 +6,7 @@ Useful when you want to communicate that process failed, with a list of sibling
repositories:
- [Grifart GitLab](https://gitlab.grifart.cz/grifart/suppressed-exceptions) [![pipeline status](https://gitlab.grifart.cz/grifart/suppressed-exceptions/badges/master/pipeline.svg)](https://gitlab.grifart.cz/grifart/suppressed-exceptions/commits/master)
- [GitHub](https://github.com/grifart/suppressed-exceptions) (mirror) ![](https://travis-ci.org/grifart/suppressed-exceptions.svg?branch=master)
- [GitHub](https://github.com/grifart/suppressed-exceptions) (mirror) [![Build Status](https://travis-ci.org/grifart/suppressed-exceptions.svg?branch=master)](https://travis-ci.org/grifart/suppressed-exceptions)
## Example usage
......
......@@ -14,6 +14,7 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */
public function addSuppressed(\Throwable ...$exceptions): void
{
foreach($exceptions as $exception) {
$this->addTextVersionOfExceptionToMessage($exception);
$this->suppressedExceptions[] = $exception;
}
}
......@@ -23,4 +24,43 @@ trait SuppressedExceptions /* implements WithSuppressedExceptions */
return $this->suppressedExceptions;
}
}
\ No newline at end of file
private function addTextVersionOfExceptionToMessage(\Throwable $throwable): void
{
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
$exception = new TestingSuppressedExceptionsException('message', 42, $previous);
$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($suppressed4 = new \Exception());
$exception->addSuppressed($suppressed4 = new \Exception('With previous', 0, $previous));
$exception->addSuppressed($suppressed5 = new TestingSuppressedExceptionsException());
// test that can be thrown
Assert::exception(function() use ($exception) {
throw $exception;
}, TestingSuppressedExceptionsException::class);
Assert::exception(
function() use ($exception) {
throw $exception;
},
TestingSuppressedExceptionsException::class,
\file_get_contents(__DIR__ . '/simple_exception-message.txt')
);
// previous
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