From 2be3e17db7f1fb4dad1ee2995e18b4136e54afb1 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 17 Oct 2020 16:23:31 +0200 Subject: [PATCH] Exceptions: Log previous exception --- src/Database/DatabaseServiceProvider.php | 9 ++++++--- src/Exceptions/Handlers/Legacy.php | 4 +++- tests/Unit/Exceptions/Handlers/LegacyTest.php | 5 ++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 715b04a4..2bee95c1 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -8,6 +8,7 @@ use Exception; use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Connection as DatabaseConnection; use PDOException; +use Throwable; class DatabaseServiceProvider extends ServiceProvider { @@ -38,7 +39,7 @@ class DatabaseServiceProvider extends ServiceProvider try { $pdo = $capsule->getConnection()->getPdo(); } catch (PDOException $e) { - $this->exitOnError(); + $this->exitOnError($e); } $this->app->instance(CapsuleManager::class, $capsule); @@ -56,10 +57,12 @@ class DatabaseServiceProvider extends ServiceProvider } /** + * @param Throwable $exception + * * @throws Exception */ - protected function exitOnError() + protected function exitOnError(Throwable $exception) { - throw new Exception('Error: Unable to connect to database'); + throw new Exception('Error: Unable to connect to database', 0, $exception); } } diff --git a/src/Exceptions/Handlers/Legacy.php b/src/Exceptions/Handlers/Legacy.php index f8ff8e62..bd78380f 100644 --- a/src/Exceptions/Handlers/Legacy.php +++ b/src/Exceptions/Handlers/Legacy.php @@ -25,12 +25,14 @@ class Legacy implements HandlerInterface */ public function report(Throwable $e) { + $previous = $e->getPrevious(); error_log(sprintf( - 'Exception: Code: %s, Message: %s, File: %s:%u, Trace: %s', + 'Exception: Code: %s, Message: %s, File: %s:%u, Previous: %s, Trace: %s', $e->getCode(), $e->getMessage(), $this->stripBasePath($e->getFile()), $e->getLine(), + $previous ? $previous->getMessage() : 'None', json_encode($e->getTrace()) )); diff --git a/tests/Unit/Exceptions/Handlers/LegacyTest.php b/tests/Unit/Exceptions/Handlers/LegacyTest.php index 7430f777..90dccbe8 100644 --- a/tests/Unit/Exceptions/Handlers/LegacyTest.php +++ b/tests/Unit/Exceptions/Handlers/LegacyTest.php @@ -39,7 +39,7 @@ class LegacyTest extends TestCase $exception = new Exception('Lorem Ipsum', 4242); $line = __LINE__ - 1; $exception2 = new Exception('Test Exception'); - $exception3 = new Exception('Mor Exceptions!'); + $exception3 = new Exception('Moar Exceptions!', 42, new Exception('Another Exception')); $logger = new TestLogger(); $logger2 = $this->createMock(TestLogger::class); $logger2->expects($this->once()) @@ -66,6 +66,9 @@ class LegacyTest extends TestCase $this->assertStringContainsString((string)$line, $logContent); $this->assertStringContainsString(__FUNCTION__, $logContent); $this->assertStringContainsString(json_encode(__CLASS__), $logContent); + $this->assertStringContainsString('Test Exception', $logContent); + $this->assertStringContainsString('Moar Exceptions!', $logContent); + $this->assertStringContainsString('Another Exception', $logContent); $this->assertTrue($logger->hasRecordThatPasses(function (array $record) use ($exception2) { $context = $record['context'];