Exceptions: Log previous exception

This commit is contained in:
Igor Scheller 2020-10-17 16:23:31 +02:00 committed by msquare
parent e74eb6eee4
commit 2be3e17db7
3 changed files with 13 additions and 5 deletions

View File

@ -8,6 +8,7 @@ use Exception;
use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection as DatabaseConnection; use Illuminate\Database\Connection as DatabaseConnection;
use PDOException; use PDOException;
use Throwable;
class DatabaseServiceProvider extends ServiceProvider class DatabaseServiceProvider extends ServiceProvider
{ {
@ -38,7 +39,7 @@ class DatabaseServiceProvider extends ServiceProvider
try { try {
$pdo = $capsule->getConnection()->getPdo(); $pdo = $capsule->getConnection()->getPdo();
} catch (PDOException $e) { } catch (PDOException $e) {
$this->exitOnError(); $this->exitOnError($e);
} }
$this->app->instance(CapsuleManager::class, $capsule); $this->app->instance(CapsuleManager::class, $capsule);
@ -56,10 +57,12 @@ class DatabaseServiceProvider extends ServiceProvider
} }
/** /**
* @param Throwable $exception
*
* @throws 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);
} }
} }

View File

@ -25,12 +25,14 @@ class Legacy implements HandlerInterface
*/ */
public function report(Throwable $e) public function report(Throwable $e)
{ {
$previous = $e->getPrevious();
error_log(sprintf( 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->getCode(),
$e->getMessage(), $e->getMessage(),
$this->stripBasePath($e->getFile()), $this->stripBasePath($e->getFile()),
$e->getLine(), $e->getLine(),
$previous ? $previous->getMessage() : 'None',
json_encode($e->getTrace()) json_encode($e->getTrace())
)); ));

View File

@ -39,7 +39,7 @@ class LegacyTest extends TestCase
$exception = new Exception('Lorem Ipsum', 4242); $exception = new Exception('Lorem Ipsum', 4242);
$line = __LINE__ - 1; $line = __LINE__ - 1;
$exception2 = new Exception('Test Exception'); $exception2 = new Exception('Test Exception');
$exception3 = new Exception('Mor Exceptions!'); $exception3 = new Exception('Moar Exceptions!', 42, new Exception('Another Exception'));
$logger = new TestLogger(); $logger = new TestLogger();
$logger2 = $this->createMock(TestLogger::class); $logger2 = $this->createMock(TestLogger::class);
$logger2->expects($this->once()) $logger2->expects($this->once())
@ -66,6 +66,9 @@ class LegacyTest extends TestCase
$this->assertStringContainsString((string)$line, $logContent); $this->assertStringContainsString((string)$line, $logContent);
$this->assertStringContainsString(__FUNCTION__, $logContent); $this->assertStringContainsString(__FUNCTION__, $logContent);
$this->assertStringContainsString(json_encode(__CLASS__), $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) { $this->assertTrue($logger->hasRecordThatPasses(function (array $record) use ($exception2) {
$context = $record['context']; $context = $record['context'];