Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Merge pull request #29 from chris-die/master
Browse files Browse the repository at this point in the history
Modify error handler to provide a detailed array to the error logger
  • Loading branch information
chris-die authored Oct 11, 2017
2 parents 82a13b5 + 1b32399 commit f482daf
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/Slim/Handlers/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,31 +215,57 @@ protected function renderJsonErrorMessage(\Exception $exception): string
*/
protected function writeToErrorLog($throwable)
{
$message = 'Slim Application Error:' . PHP_EOL;
$message .= $this->renderThrowableAsText($throwable);
// $message = 'Slim Application Error:' . PHP_EOL;
// $message .= $this->renderThrowableAsText($throwable);
// while ($throwable = $throwable->getPrevious()) {
// $message .= PHP_EOL . 'Previous error:' . PHP_EOL;
// $message .= $this->renderThrowableAsText($throwable);
// }
$error = $this->renderThrowableAsArray($throwable);
while ($throwable = $throwable->getPrevious()) {
$message .= PHP_EOL . 'Previous error:' . PHP_EOL;
$message .= $this->renderThrowableAsText($throwable);
if (isset($error['previous'])) {
$error['previous'] = [];
}
$error['previous'][] .= $this->renderThrowableAsArray($throwable);
}
$this->logError($message);

$this->logger->critical(
'Slim Application Error',
array_merge(
$error,
[
'request_method' => $this->requestMethod,
'request_path' => $this->requestPath,
'request_query_string' => $this->requestQueryString
]
)
);
}

/**
* Wraps the error_log function so that this can be easily tested
*
* @todo Specify void return type in PHP 7.1
* @param \Exception|\Throwable $throwable
*
* @param $message
* @return array
*/
protected function logError($message)
private function renderThrowableAsArray($throwable): array
{
$this->logger->critical(
$message,
[
$this->requestMethod,
$this->requestPath,
$this->requestQueryString
]
);
$error = [];
$error['type'] = get_class($throwable);
if ($code = $throwable->getCode()) {
$error['code'] = $code;
}
if ($message = $throwable->getMessage()) {
$error['message'] = htmlentities($message);
}
if ($file = $throwable->getFile()) {
$error['file'] = $file;
}
if ($line = $throwable->getLine()) {
$error['line'] = $line;
}
if ($trace = $throwable->getTraceAsString()) {
$error['trace'] = explode("\n", $throwable->getTraceAsString());
}
return $error;
}
}

0 comments on commit f482daf

Please sign in to comment.