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

160 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
*/
namespace Snowflake\Error;
use Exception;
use HttpServer\IInterface\IFormatter;
use Snowflake\Abstracts\Component;
use Snowflake\Core\JSON;
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
{
/** @var IFormatter $message */
private $message = NULL;
public $action;
public $category = 'app';
/**
* 错误处理注册
*/
public function register()
{
ini_set('display_errors', 0);
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']);
}
/**
* @param Exception $exception
*
* @throws Exception
*/
2020-09-22 14:41:50 +08:00
public function exceptionHandler(Exception $exception)
2020-08-31 01:27:08 +08:00
{
$this->category = 'exception';
2020-09-03 11:39:20 +08:00
$event = Snowflake::app()->event;
2020-08-31 01:27:08 +08:00
$event->trigger(Event::RELEASE_ALL);
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
}
/**
* @throws Exception
*
* 以异常形式抛出错误,防止执行后续程序
*/
public function errorHandler()
{
$error = func_get_args();
if (strpos($error[2], 'vendor/Reboot.php') !== FALSE) {
return;
}
$path = ['file' => $error[2], 'line' => $error[3]];
if ($error[0] === 0) {
$error[0] = 500;
}
2020-09-07 12:04:43 +08:00
$data = JSON::to(500, $error[1], $path);
2020-08-31 01:27:08 +08:00
2020-09-22 14:41:50 +08:00
logger()->error($data, 'error');
2020-08-31 01:27:08 +08:00
2020-09-03 11:39:20 +08:00
$event = Snowflake::app()->event;
2020-08-31 01:27:08 +08:00
$event->trigger(Event::RELEASE_ALL);
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
*/
public function sendError($message, $file, $line, $code = 500)
{
$path = ['file' => $file, 'line' => $line];
$data = JSON::to($code, $this->category . ': ' . $message, $path);
2020-09-22 14:41:50 +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
*/
public function getErrorMessage()
{
$message = $this->message;
$this->message = NULL;
return $message->getData();
}
/**
* @return bool
*/
public function getAsError()
{
return $this->message !== NULL;
}
/**
* @param $message
* @param $category
*
* @throws Exception
*/
public function writer($message, $category = 'app')
{
2020-09-22 14:41:50 +08:00
logger()->debug($message, $category);
2020-08-31 01:27:08 +08:00
}
}