Files
kiri-core/kiri-engine/Error/ErrorHandler.php
T

214 lines
4.6 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/26 0026
* Time: 10:00
*/
declare(strict_types=1);
namespace Kiri\Error;
2022-06-22 19:02:30 +08:00
use Closure;
2022-01-09 03:50:38 +08:00
use Exception;
2022-02-14 17:59:56 +08:00
use Kiri;
2022-01-09 03:50:38 +08:00
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
2022-06-22 10:53:59 +08:00
use Kiri\Annotation\Inject;
2022-01-09 03:50:38 +08:00
use Kiri\Events\EventDispatch;
2022-01-10 11:39:56 +08:00
use Kiri\Message\Events\OnAfterRequest;
2022-02-14 17:59:56 +08:00
use Kiri\Message\Handler\Formatter\IFormatter;
2022-06-22 10:53:59 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Kiri\Server\Events\OnShutdown;
use ReflectionException;
2022-01-09 03:50:38 +08:00
/**
* Class ErrorHandler
*
2022-01-12 14:10:33 +08:00
* @package Kiri\Base
2022-01-09 03:50:38 +08:00
* @property-read $asError
*/
class ErrorHandler extends Component implements ErrorInterface
{
/** @var ?IFormatter $message */
private ?IFormatter $message = NULL;
2022-06-22 16:50:37 +08:00
/**
* @var string
*/
2022-01-09 03:50:38 +08:00
public string $category = 'app';
2022-06-22 10:53:59 +08:00
/**
* @var EventDispatch
*/
#[Inject(EventDispatch::class)]
public EventDispatch $dispatch;
2022-01-09 03:50:38 +08:00
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
*/
2022-06-22 19:02:30 +08:00
public function registerExceptionHandler(null|array|Closure $callback): void
2022-06-22 16:57:11 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'exceptionHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
set_exception_handler($callback);
2022-06-22 16:57:11 +08:00
}
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
2022-01-09 03:50:38 +08:00
*/
2022-06-22 19:02:30 +08:00
public function registerErrorHandler(null|array|Closure $callback): void
2022-01-09 03:50:38 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'errorHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
set_error_handler($callback);
2022-01-09 03:50:38 +08:00
}
2022-06-22 16:57:11 +08:00
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
*/
2022-06-22 19:02:30 +08:00
public function registerShutdownHandler(null|array|Closure $callback): void
2022-06-22 16:57:11 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'shutdown'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
register_shutdown_function($callback);
2022-06-22 16:57:11 +08:00
}
2022-01-09 03:50:38 +08:00
/**
2022-06-22 10:53:59 +08:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-06-22 10:53:59 +08:00
public function shutdown(): void
2022-01-09 03:50:38 +08:00
{
$lastError = error_get_last();
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
return;
}
$this->category = 'shutdown';
$messages = explode(PHP_EOL, $lastError['message']);
$message = array_shift($messages);
2022-06-22 10:53:59 +08:00
$this->dispatch->dispatch(new OnShutdown());
2022-01-09 03:50:38 +08:00
$this->sendError($message, $lastError['file'], $lastError['line']);
}
/**
* @param \Throwable $exception
*
2022-06-22 10:53:59 +08:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
public function exceptionHandler(\Throwable $exception)
{
$this->category = 'exception';
2022-06-22 10:53:59 +08:00
$this->dispatch->dispatch(new OnAfterRequest());
2022-01-09 03:50:38 +08:00
2022-02-14 17:59:56 +08:00
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
2022-01-09 03:50:38 +08:00
}
/**
2022-06-22 10:53:59 +08:00
* @throws \ErrorException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
*/
public function errorHandler()
{
$error = func_get_args();
2022-02-14 17:59:56 +08:00
$path = ['file' => $error[2], 'line' => $error[3]];
2022-01-09 03:50:38 +08:00
if ($error[0] === 0) {
$error[0] = 500;
}
$data = Json::to(500, $error[1], $path);
2022-02-23 16:32:08 +08:00
$this->logger->error('On error handler', [$data]);
2022-01-09 03:50:38 +08:00
2022-06-22 10:53:59 +08:00
$this->dispatch->dispatch(new OnAfterRequest());
2022-01-09 03:50:38 +08:00
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
/**
* @param $message
* @param $file
* @param $line
* @param int $code
* @return false|string
* @throws Exception
*/
2022-06-22 16:29:42 +08:00
public function sendError($message, $file, $line, int $code = 500): bool|string
2022-01-09 03:50:38 +08:00
{
$path = ['file' => $file, 'line' => $line];
$data = Json::to($code, $this->category . ': ' . $message, $path);
2022-02-14 17:59:56 +08:00
file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND);
2022-01-09 03:50:38 +08:00
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
* @param string $category
*
* @throws Exception
*/
public function writer($message, string $category = 'app')
{
2022-06-22 16:29:42 +08:00
Kiri::getLogger()->debug($category, [$message]);
2022-01-09 03:50:38 +08:00
}
}