Skip to content

Commit

Permalink
fix: use plural for variables and separate logic to save log
Browse files Browse the repository at this point in the history
  • Loading branch information
jok3rcito0 committed May 25, 2023
1 parent 4639894 commit 78e2c4d
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Middleware/LogExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$response = $handler->handle($request);
$body = json_decode($response->getBody()->getContents());

$error = $this->getErrors($body);
$errors = $this->getErrors($body);

if ($response instanceof NonCriticalDomainException) {
Log::error($error, [], self::EXCEPTIONS_CHANNEL);
} else {
Log::critical($error, [], self::EXCEPTIONS_CHANNEL);
foreach ($errors as $error) {
$this->saveLog($error->message, (array) $error, $response);
}

return $response;
Expand All @@ -36,14 +34,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}

private function getErrors($body)
private function getErrors($body): array
{
if ($body === null && json_last_error() !== JSON_ERROR_NONE){
$error = 'Error: Invalid response';
$errors[] = ['message' => 'Error: Invalid response'];
}else{
$error = (empty($body->errors) || !isset($body->errors)) ? $body : $body->errors;
$errors = (empty($body->errors) || !isset($body->errors)) ? [0 => (array) $body] : $body->errors;
}

return $error;
return (array) $errors;
}

private function saveLog($message, $error, $handle)
{
if ($handle instanceof NonCriticalDomainException) {
Log::error($message, $error, self::EXCEPTIONS_CHANNEL);
} else {
Log::critical($message, $error, self::EXCEPTIONS_CHANNEL);
}
}
}

0 comments on commit 78e2c4d

Please sign in to comment.