From b7513d573d8f2320afbe72bfb54d6bcb90d12794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 24 Apr 2023 17:14:15 +0200 Subject: [PATCH] Add stack trace for previous exceptions to bootstrap error message --- src/TextUI/Command.php | 25 +++++++++++++++---- tests/end-to-end/regression/4620.phpt | 4 +++ .../end-to-end/regression/4620/bootstrap.php | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/TextUI/Command.php b/src/TextUI/Command.php index 48243caa8ca..9d5e1e13762 100644 --- a/src/TextUI/Command.php +++ b/src/TextUI/Command.php @@ -568,15 +568,30 @@ protected function handleBootstrap(string $filename): void $this->exitWithErrorMessage($t->getMessage()); } - $this->exitWithErrorMessage( - sprintf( - 'Error in bootstrap script: %s:%s%s%s%s', + $message = sprintf( + 'Error in bootstrap script: %s:%s%s%s%s', + get_class($t), + PHP_EOL, + $t->getMessage(), + PHP_EOL, + $t->getTraceAsString() + ); + + while ($t = $t->getPrevious()) { + $message .= sprintf( + '%s%sPrevious error: %s:%s%s%s%s', + PHP_EOL, + PHP_EOL, get_class($t), PHP_EOL, $t->getMessage(), PHP_EOL, - $t->getTraceAsString() - ) + $t->getTraceAsString(), + ); + } + + $this->exitWithErrorMessage( + $message ); } } diff --git a/tests/end-to-end/regression/4620.phpt b/tests/end-to-end/regression/4620.phpt index a774d70449d..b72591933f4 100644 --- a/tests/end-to-end/regression/4620.phpt +++ b/tests/end-to-end/regression/4620.phpt @@ -19,3 +19,7 @@ PHPUnit %s by Sebastian Bergmann and contributors. Error in bootstrap script: PHPUnit\TestFixture\MyException: Big boom. Big bada boom. %a + +Previous error: Exception: +Previous boom. +%a diff --git a/tests/end-to-end/regression/4620/bootstrap.php b/tests/end-to-end/regression/4620/bootstrap.php index 1ebcc1818a1..706c9d6e883 100644 --- a/tests/end-to-end/regression/4620/bootstrap.php +++ b/tests/end-to-end/regression/4620/bootstrap.php @@ -15,4 +15,4 @@ final class MyException extends Exception { } -throw new MyException('Big boom. Big bada boom.'); +throw new MyException('Big boom. Big bada boom.', 0, new Exception('Previous boom.'));