This commit is contained in:
2023-04-16 01:45:34 +08:00
parent 46baac8bbd
commit 11c21f01a2
35 changed files with 36 additions and 1377 deletions
+6 -31
View File
@@ -14,14 +14,11 @@ use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch;
use Kiri\Message\Events\OnAfterRequest;
use Kiri\Message\Handler\Formatter\IFormatter;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Kiri\Server\Events\OnShutdown;
use ReflectionException;
use Kiri\Di\Inject\Container;
/**
* Class ErrorHandler
@@ -32,10 +29,6 @@ use ReflectionException;
class ErrorHandler extends Component implements ErrorInterface
{
/** @var ?IFormatter $message */
private ?IFormatter $message = NULL;
/**
* @var string
*/
@@ -45,7 +38,7 @@ class ErrorHandler extends Component implements ErrorInterface
/**
* @var EventDispatch
*/
#[Inject(EventDispatch::class)]
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
@@ -114,7 +107,7 @@ class ErrorHandler extends Component implements ErrorInterface
$message = array_shift($messages);
$this->dispatch->dispatch(new OnShutdown());
$this->dispatch->dispatch(new Kiri\Events\OnSystemError());
$this->sendError($message, $lastError['file'], $lastError['line']);
}
@@ -125,14 +118,13 @@ class ErrorHandler extends Component implements ErrorInterface
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception
*/
public function exceptionHandler(\Throwable $exception)
{
$this->category = 'exception';
$this->dispatch->dispatch(new OnAfterRequest());
$this->dispatch->dispatch(new Kiri\Events\OnSystemError());
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
}
@@ -158,7 +150,7 @@ class ErrorHandler extends Component implements ErrorInterface
$this->logger->error('On error handler', [$data]);
$this->dispatch->dispatch(new OnAfterRequest());
$this->dispatch->dispatch(new Kiri\Events\OnSystemError());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
@@ -181,24 +173,7 @@ class ErrorHandler extends Component implements ErrorInterface
return $data;
}
/**
* @return mixed
*/
public function getErrorMessage(): mixed
{
$message = $this->message;
$this->message = NULL;
return $message->getData();
}
/**
* @return bool
*/
public function getAsError(): bool
{
return $this->message !== NULL;
}
/**
* @param $message
-53
View File
@@ -1,53 +0,0 @@
<?php
namespace Kiri\Error;
use Exception;
use Kiri\Message\Aspect\OnAspectInterface;
use Kiri\Message\Aspect\OnJoinPointInterface;
use Kiri\Message\Constrict\RequestInterface;
use Kiri;
use Psr\Log\LoggerInterface;
/**
* Class LoggerAspect
* @package Kiri\Error
*/
class LoggerAspect implements OnAspectInterface
{
/**
* @param OnJoinPointInterface $joinPoint
* @return mixed
* @throws Exception
*/
public function process(OnJoinPointInterface $joinPoint): mixed
{
$time = microtime(true);
$response = $joinPoint->process();
$this->print_runtime($time);
return $response;
}
/**
* @param $startTime
* @throws Exception
*/
private function print_runtime($startTime)
{
$request = Kiri::getDi()->get(RequestInterface::class);
$runTime = round(microtime(true) - $startTime, 6);
$logger = Kiri::getDi()->get(LoggerInterface::class);
$logger->debug(sprintf('run %s use time %6f', $request->getUri()->__toString(), $runTime));
}
}
+2
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Kiri\Error;
use Kiri\Abstracts\Logger;