63 lines
2.2 KiB
PHP
63 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Negundo\Client\Util;
|
|
|
|
use Negundo\Client\DataInterface;
|
|
|
|
class ExceptionHandler {
|
|
|
|
public ? DataInterface $dataManipulator;
|
|
|
|
public function __construct(DataInterface $dataManipulator = null)
|
|
{
|
|
$this->dataManipulator = $dataManipulator;
|
|
}
|
|
|
|
public function extractExceptionData(\Throwable $ex, array $serverData, array $postData, $dataManipulator = null) : array
|
|
{
|
|
$serverData = [
|
|
'HTTPS' => $serverData['HTTPS'] ?? false,
|
|
'HTTP_HOST' => $serverData['HTTP_HOST'] ?? "",
|
|
'REQUEST_URI' => $serverData['REQUEST_URI'] ?? "",
|
|
'HTTP_USER_AGENT' => $serverData['HTTP_USER_AGENT'] ?? null,
|
|
'REMOTE_ADDR' => $serverData['REMOTE_ADDR'] ?? null,
|
|
];
|
|
|
|
$post = [
|
|
'code' => $ex->getCode(),
|
|
'file' => $ex->getFile(),
|
|
'line' => $ex->getLine(),
|
|
'type' => $ex::class,
|
|
'message' => $ex->getMessage(),
|
|
'url' => ( ( 'on' === $serverData['HTTPS'] ) ? 'https' : 'http' ) . '://' . $serverData['HTTP_HOST'] . $serverData["REQUEST_URI"],
|
|
'backtrace' => json_encode($ex->getTrace()),
|
|
'backtrace_string' => $ex->getTraceAsString(),
|
|
'source' => ( new SourceCodeFormatter() )->generateFromException($ex),
|
|
'hits' => [
|
|
'user_agent' => $serverData['HTTP_USER_AGENT'] ?? "???",
|
|
'sent_at' => date('Y-m-d H:i:s'),
|
|
'request_body_vars' => array_keys($postData),
|
|
'remote_addr' => $serverData['REMOTE_ADDR'],
|
|
],
|
|
'data' => [],
|
|
];
|
|
|
|
if ( $dataManipulator ) {
|
|
$post['data'] = $dataManipulator->getData();
|
|
$post['hits'] = array_merge($post['hits'], $dataManipulator->getHits());
|
|
|
|
$dataManipulator->run($post);
|
|
}
|
|
|
|
$post['data'] = json_encode($post['data'] ?? null);
|
|
$post['hits'] = json_encode($post['hits'] ?? null);
|
|
|
|
return $post;
|
|
}
|
|
|
|
public function hash(\Throwable $ex) : string
|
|
{
|
|
return md5(implode('-', [ $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getCode() ]));
|
|
}
|
|
}
|