This commit is contained in:
2023-07-26 17:26:47 +08:00
parent 8d4dd1bda9
commit c062a8f6a3
2 changed files with 133 additions and 154 deletions
+14 -4
View File
@@ -1266,13 +1266,23 @@ if (!function_exists('throwable')) {
*/ */
function throwable(\Throwable|\Error $throwable): string function throwable(\Throwable|\Error $throwable): string
{ {
$message = "\033[31m" . $throwable->getMessage() . "\033[0m" . PHP_EOL . $message = "\033[31m" . $throwable::class . ' ' . $throwable->getMessage() . "\033[0m" . PHP_EOL;
' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL; $message .= $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
$file = $throwable->getFile();
$line = $throwable->getLine();
foreach ($throwable->getTrace() as $value) { foreach ($throwable->getTrace() as $value) {
if (!isset($value['file'])) { 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; return $message;
} }
+5 -36
View File
@@ -97,15 +97,9 @@ class ErrorHandler extends Component implements ErrorInterface
return; return;
} }
$this->category = 'shutdown'; error("\033[31m" . $lastError['message'] . "\033[0m" . $lastError['file'] . " at line " . $lastError['line'] . PHP_EOL);
$messages = explode(PHP_EOL, $lastError['message']);
$message = array_shift($messages);
event(new Kiri\Events\OnSystemError()); event(new Kiri\Events\OnSystemError());
$this->sendError($message, $lastError['file'], $lastError['line']);
} }
@@ -120,6 +114,8 @@ class ErrorHandler extends Component implements ErrorInterface
{ {
$this->category = 'exception'; $this->category = 'exception';
Kiri::getLogger()->error($exception, []);
event(new Kiri\Events\OnSystemError()); event(new Kiri\Events\OnSystemError());
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
@@ -136,17 +132,8 @@ class ErrorHandler extends Component implements ErrorInterface
{ {
$error = func_get_args(); $error = func_get_args();
$path = ['file' => $error[2], 'line' => $error[3]]; error("\033[31m" . $error[1] . "\033[0m" . $error[2] . " at line " . $error[3] . PHP_EOL);
if ($error[0] === 0) {
$error[0] = 500;
}
$data = Json::jsonFail($error[1], 500, $path);
if (!empty($data)) {
error($data, []);
}
event(new Kiri\Events\OnSystemError()); event(new Kiri\Events\OnSystemError());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
@@ -162,24 +149,6 @@ class ErrorHandler extends Component implements ErrorInterface
*/ */
public function sendError($message, $file, $line, int $code = 500): bool|string public function sendError($message, $file, $line, int $code = 500): bool|string
{ {
$path = ['file' => $file, 'line' => $line]; return "";
$data = Json::jsonFail($this->category . ': ' . $message, $code, $path);
file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND);
return $data;
}
/**
* @param $message
* @param string $category
*
* @throws Exception
*/
public function writer($message, string $category = 'app')
{
Kiri::getLogger()->debug($category, [$message]);
} }
} }