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

157 lines
2.9 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/26 0026
* Time: 10:00
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Error;
2020-08-31 01:27:08 +08:00
use Exception;
2021-09-24 13:47:34 +08:00
use Http\Handler\Formatter\IFormatter;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
2021-08-13 14:58:58 +08:00
use Kiri\Events\EventDispatch;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-08-13 14:58:58 +08:00
use Server\Events\OnAfterRequest;
2020-08-31 01:27:08 +08:00
/**
* Class ErrorHandler
*
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Base
2020-08-31 01:27:08 +08:00
* @property-read $asError
*/
class ErrorHandler extends Component implements ErrorInterface
{
2020-10-26 14:33:12 +08:00
/** @var ?IFormatter $message */
private ?IFormatter $message = NULL;
2020-08-31 01:27:08 +08:00
2020-10-26 14:31:50 +08:00
public string $category = 'app';
2020-08-31 01:27:08 +08:00
/**
* 错误处理注册
*/
public function register()
{
2021-05-04 02:29:59 +08:00
// ini_set('display_errors', '1');
2020-08-31 01:27:08 +08:00
set_exception_handler([$this, 'exceptionHandler']);
if (defined('HHVM_VERSION')) {
set_error_handler([$this, 'errorHandler']);
} else {
set_error_handler([$this, 'errorHandler']);
}
register_shutdown_function([$this, 'shutdown']);
}
/**
* @throws Exception
*/
public function shutdown()
{
$lastError = error_get_last();
2020-10-08 03:37:44 +08:00
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
2020-08-31 01:27:08 +08:00
return;
}
$this->category = 'shutdown';
$messages = explode(PHP_EOL, $lastError['message']);
$message = array_shift($messages);
$this->sendError($message, $lastError['file'], $lastError['line']);
}
/**
2020-11-06 16:47:17 +08:00
* @param \Throwable $exception
2020-08-31 01:27:08 +08:00
*
* @throws Exception
*/
2020-11-06 16:47:17 +08:00
public function exceptionHandler(\Throwable $exception)
2020-08-31 01:27:08 +08:00
{
$this->category = 'exception';
2021-08-13 14:58:58 +08:00
di(EventDispatch::class)->dispatch(new OnAfterRequest());
2020-08-31 01:27:08 +08:00
2021-05-04 02:29:33 +08:00
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
2020-08-31 01:27:08 +08:00
}
2021-03-01 15:52:11 +08:00
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*
* 以异常形式抛出错误,防止执行后续程序
*/
public function errorHandler()
{
$error = func_get_args();
2021-05-04 02:29:33 +08:00
$path = ['file' => $error[2], 'line' => $error[3]];
2020-08-31 01:27:08 +08:00
if ($error[0] === 0) {
$error[0] = 500;
}
2020-12-24 11:12:23 +08:00
$data = Json::to(500, $error[1], $path);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
Kiri::app()->error($data, 'error');
2020-08-31 01:27:08 +08:00
2021-08-13 14:58:58 +08:00
di(EventDispatch::class)->dispatch(new OnAfterRequest());
2020-08-31 01:27:08 +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
*/
2020-12-17 14:09:14 +08:00
public function sendError($message, $file, $line, $code = 500): bool|string
2020-08-31 01:27:08 +08:00
{
$path = ['file' => $file, 'line' => $line];
2020-12-24 11:12:23 +08:00
$data = Json::to($code, $this->category . ': ' . $message, $path);
2020-08-31 01:27:08 +08:00
2021-08-04 11:15:46 +08:00
write($data, $this->category);
2020-08-31 01:27:08 +08:00
2021-08-04 11:15:46 +08:00
return $data;
2020-08-31 01:27:08 +08:00
}
/**
* @return mixed
*/
2020-12-17 14:09:14 +08:00
public function getErrorMessage(): mixed
2020-08-31 01:27:08 +08:00
{
$message = $this->message;
$this->message = NULL;
return $message->getData();
}
/**
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function getAsError(): bool
2020-08-31 01:27:08 +08:00
{
return $this->message !== NULL;
}
/**
* @param $message
2021-07-13 17:06:08 +08:00
* @param string $category
2020-08-31 01:27:08 +08:00
*
* @throws Exception
*/
2021-07-13 17:06:08 +08:00
public function writer($message, string $category = 'app')
2020-08-31 01:27:08 +08:00
{
2021-08-11 01:04:57 +08:00
Kiri::app()->debug($message, $category);
2020-08-31 01:27:08 +08:00
}
}