Skip to content

Commit

Permalink
refactor: Refactor LogExceptions middleware to handle and log exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jok3rcito0 committed May 23, 2023
1 parent c65f921 commit 05d1ec8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Middleware/LogExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ class LogExceptions implements MiddlewareInterface

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$error = $handler->handle($request);
$body = json_decode($error->getBody()->getContents(), false);
$error_handle = $handler->handle($request);
$body = json_decode($$error_handle->getBody()->getContents(), false);

$error = ($body === null && json_last_error() !== JSON_ERROR_NONE)
? 'Error: Invalid response'
: (empty($body->errors) || !isset($body->errors)) ? $body : $body->errors;

if ($error instanceof NonCriticalDomainException) {
Log::error($body->errors, [], self::EXCEPTIONS_CHANNEL);
Log::error($error, [], self::EXCEPTIONS_CHANNEL);
} else {
Log::critical($body->errors, [], self::EXCEPTIONS_CHANNEL);
Log::critical($error, [], self::EXCEPTIONS_CHANNEL);
}

return $error;
return $error_handle;
}
}

0 comments on commit 05d1ec8

Please sign in to comment.