qqq
This commit is contained in:
+14
-4
@@ -1266,13 +1266,23 @@ if (!function_exists('throwable')) {
|
||||
*/
|
||||
function throwable(\Throwable|\Error $throwable): string
|
||||
{
|
||||
$message = "\033[31m" . $throwable->getMessage() . "\033[0m" . PHP_EOL .
|
||||
' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
|
||||
$message = "\033[31m" . $throwable::class . ' ' . $throwable->getMessage() . "\033[0m" . PHP_EOL;
|
||||
$message .= $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
|
||||
|
||||
$file = $throwable->getFile();
|
||||
$line = $throwable->getLine();
|
||||
|
||||
foreach ($throwable->getTrace() as $value) {
|
||||
if (!isset($value['file'])) {
|
||||
continue;
|
||||
$value['file'] = $file;
|
||||
}
|
||||
$message .= ' ' . $value['file'] . " -> " . (isset($value['class']) ? $value['class'] . '::' : '') . ($value['function'] ?? 'Closure') . "(" . $value['line'] . ")" . PHP_EOL;
|
||||
if (!isset($value['line'])) {
|
||||
$value['line'] = $line;
|
||||
}
|
||||
$file = $value['file'];
|
||||
$line = $value['line'];
|
||||
|
||||
$message .= $value['file'] . ' -> ' . (isset($value['class']) ? $value['class'] . '::' : '') . ($value['function'] ?? 'Closure') . "(" . implode(",", $value['args'] ?? []) . ")" . ' line ' . $line . PHP_EOL;
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
+119
-150
@@ -28,158 +28,127 @@ use Kiri\Di\Inject\Container;
|
||||
*/
|
||||
class ErrorHandler extends Component implements ErrorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $category = 'app';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $category = 'app';
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerExceptionHandler(null|array|Closure $callback): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerErrorHandler(null|array|Closure $callback): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerShutdownHandler(null|array|Closure $callback): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown(): void
|
||||
{
|
||||
$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);
|
||||
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
|
||||
$this->sendError($message, $lastError['file'], $lastError['line']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exceptionHandler(\Throwable $exception): void
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \ErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function errorHandler()
|
||||
{
|
||||
$error = func_get_args();
|
||||
|
||||
$path = ['file' => $error[2], 'line' => $error[3]];
|
||||
|
||||
if ($error[0] === 0) {
|
||||
$error[0] = 500;
|
||||
}
|
||||
|
||||
$data = Json::jsonFail($error[1], 500, $path);
|
||||
|
||||
if (!empty($data)) {
|
||||
error($data, []);
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerExceptionHandler(null|array|Closure $callback): void
|
||||
{
|
||||
if (empty($callback)) {
|
||||
$callback = [$this, 'exceptionHandler'];
|
||||
} else if (is_array($callback) && is_string($callback[0])) {
|
||||
$callback[0] = Kiri::getDi()->get($callback[0]);
|
||||
}
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
|
||||
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, int $code = 500): bool|string
|
||||
{
|
||||
$path = ['file' => $file, 'line' => $line];
|
||||
|
||||
$data = Json::jsonFail($this->category . ': ' . $message, $code, $path);
|
||||
|
||||
file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND);
|
||||
|
||||
return $data;
|
||||
}
|
||||
set_exception_handler($callback);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function writer($message, string $category = 'app')
|
||||
{
|
||||
Kiri::getLogger()->debug($category, [$message]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerErrorHandler(null|array|Closure $callback): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure|null $callback
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerShutdownHandler(null|array|Closure $callback): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown(): void
|
||||
{
|
||||
$lastError = error_get_last();
|
||||
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
|
||||
return;
|
||||
}
|
||||
|
||||
error("\033[31m" . $lastError['message'] . "\033[0m" . $lastError['file'] . " at line " . $lastError['line'] . PHP_EOL);
|
||||
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exceptionHandler(\Throwable $exception): void
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
Kiri::getLogger()->error($exception, []);
|
||||
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \ErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function errorHandler()
|
||||
{
|
||||
$error = func_get_args();
|
||||
|
||||
error("\033[31m" . $error[1] . "\033[0m" . $error[2] . " at line " . $error[3] . PHP_EOL);
|
||||
|
||||
event(new Kiri\Events\OnSystemError());
|
||||
|
||||
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, int $code = 500): bool|string
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user