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

162 lines
3.0 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
namespace Snowflake\Error;
use Exception;
use HttpServer\IInterface\IFormatter;
use Snowflake\Abstracts\Component;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2020-08-31 01:27:08 +08:00
use Snowflake\Event;
use Snowflake\Snowflake;
/**
* Class ErrorHandler
*
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\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()
{
2020-10-30 01:05:30 +08:00
ini_set('display_errors', '0');
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;
}
2021-05-04 02:29:33 +08:00
var_dump($lastError);
2020-08-31 01:27:08 +08:00
$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-04-27 15:57:50 +08:00
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
2020-08-31 01:27:08 +08:00
2021-05-04 02:29:33 +08:00
var_dump($exception);
$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
var_dump(func_get_args());
$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-02-20 15:38:48 +08:00
Snowflake::app()->error($data, 'error');
2020-08-31 01:27:08 +08:00
2021-04-27 15:57:50 +08:00
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
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-02-20 15:45:48 +08:00
logger()->trance($data, $this->category);
2020-08-31 01:27:08 +08:00
2020-08-31 12:40:55 +08:00
return response()->send($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
* @param $category
*
* @throws Exception
*/
public function writer($message, $category = 'app')
{
2021-02-20 15:38:48 +08:00
Snowflake::app()->debug($message, $category);
2020-08-31 01:27:08 +08:00
}
}