Skip to content

Commit

Permalink
PsrLogErrorHandler: always check an exception wrapped inside Snapshot…
Browse files Browse the repository at this point in the history
…Exception
  • Loading branch information
mabar authored and f3l1x committed Sep 24, 2019
1 parent d6d0a6b commit 5fdf57c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/ErrorHandler/PsrLogErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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);
}

}
40 changes: 38 additions & 2 deletions tests/cases/ErrorHandler/PsrLogErrorHandler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

0 comments on commit 5fdf57c

Please sign in to comment.