From 5fdf57cc54a4b97c186df751b346a8ac9f83a7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Sat, 21 Sep 2019 01:00:20 +0200 Subject: [PATCH] PsrLogErrorHandler: always check an exception wrapped inside SnapshotException --- src/ErrorHandler/PsrLogErrorHandler.php | 13 ++++-- .../ErrorHandler/PsrLogErrorHandler.phpt | 40 ++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/ErrorHandler/PsrLogErrorHandler.php b/src/ErrorHandler/PsrLogErrorHandler.php index 6c5f10aa..e7dd4782 100644 --- a/src/ErrorHandler/PsrLogErrorHandler.php +++ b/src/ErrorHandler/PsrLogErrorHandler.php @@ -23,8 +23,15 @@ public function __construct(LoggerInterface $logger) public function handle(Throwable $error): ApiResponse { - // Log exception only if it's not designed to be displayed or as a state snapshot - if (!$error instanceof ApiException && !$error instanceof SnapshotException) { + // Unwrap error from snapshot for logging + $originalError = $error; + + if ($error instanceof SnapshotException) { + $error = $error->getPrevious(); + } + + // Log exception only if it's not designed to be displayed + if (!$error instanceof ApiException) { $this->logger->error($error->getMessage(), ['exception' => $error]); } @@ -35,7 +42,7 @@ public function handle(Throwable $error): ApiResponse $this->logger->log($level, $previous->getMessage(), ['exception' => $previous]); } - return parent::handle($error); + return parent::handle($originalError); } } diff --git a/tests/cases/ErrorHandler/PsrLogErrorHandler.phpt b/tests/cases/ErrorHandler/PsrLogErrorHandler.phpt index a9f203c2..53611d24 100644 --- a/tests/cases/ErrorHandler/PsrLogErrorHandler.phpt +++ b/tests/cases/ErrorHandler/PsrLogErrorHandler.phpt @@ -80,11 +80,47 @@ test(function (): void { test(function (): void { $logger = new TestLogger(); $handler = new PsrLogErrorHandler($logger); + + // Api exception, without previous, not loggable $handler->handle(new SnapshotException( - new ClientErrorException('test'), + new ClientErrorException('client'), new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal()), new ApiResponse(Psr7ResponseFactory::fromGlobal()) )); - Assert::same([], $logger->records); + $genericException = new Exception('generic'); + + // Api exception, with previous, loggable + $handler->handle(new SnapshotException( + new ClientErrorException('client', 400, $genericException), + new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal()), + new ApiResponse(Psr7ResponseFactory::fromGlobal()) + )); + + // Generic exception, loggable + $handler->handle(new SnapshotException( + $genericException, + new ApiRequest(Psr7ServerRequestFactory::fromSuperGlobal()), + new ApiResponse(Psr7ResponseFactory::fromGlobal()) + )); + + Assert::same( + [ + [ + 'level' => 'debug', + 'message' => 'generic', + 'context' => [ + 'exception' => $genericException, + ], + ], + [ + 'level' => 'error', + 'message' => 'generic', + 'context' => [ + 'exception' => $genericException, + ], + ], + ], + $logger->records + ); });