Created
January 17, 2022 13:21
-
-
Save andreyserdjuk/9e52c94dfb3e2cd08c9602f077b5ec7d to your computer and use it in GitHub Desktop.
flatten parent exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function a() { | |
throw new \LogicException('bad logic in A'); | |
} | |
function b() { | |
try { | |
a(); | |
} catch (\LogicException $e) { | |
throw new \RuntimeException('Runtime exception from B: ' . $e->getMessage(), 0, $e); | |
} | |
} | |
function c() { | |
b(); | |
} | |
try { | |
c(); | |
} catch (\RuntimeException $e) { | |
$messages = []; | |
$exception = $e; | |
do { | |
$messages[] = sprintf( | |
"%s was thrown with message: \"%s\"\r\n%s", | |
get_class($exception), | |
$exception->getMessage(), | |
$exception->getTraceAsString() | |
); | |
$exception = $exception->getPrevious(); | |
} while ($exception !== null); | |
echo implode("\r\n==========\r\n", $messages); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will render:
The same approach
\Symfony\Component\ErrorHandler\Exception\FlattenException::getAllPrevious()