category = 'shutdown'; $messages = explode(PHP_EOL, $lastError['message']); $message = array_shift($messages); $this->sendError($message, $lastError['file'], $lastError['line']); } /** * @param \Throwable $exception * * @throws Exception */ public function exceptionHandler(\Throwable $exception) { $this->category = 'exception'; $event = Snowflake::app()->getEvent(); $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); } /** * @throws Exception * * 以异常形式抛出错误,防止执行后续程序 */ public function errorHandler() { $error = func_get_args(); if (str_contains($error[2], 'vendor/Reboot.php')) { return; } $path = ['file' => $error[2], 'line' => $error[3]]; if ($error[0] === 0) { $error[0] = 500; } $data = Json::to(500, $error[1], $path); logger()->error($data, 'error'); $event = Snowflake::app()->getEvent(); $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); 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): bool|string { $path = ['file' => $file, 'line' => $line]; $data = Json::to($code, $this->category . ': ' . $message, $path); logger()->trance($data, $this->category); return response()->send($data); } /** * @return mixed */ public function getErrorMessage(): mixed { $message = $this->message; $this->message = NULL; return $message->getData(); } /** * @return bool */ public function getAsError(): bool { return $this->message !== NULL; } /** * @param $message * @param $category * * @throws Exception */ public function writer($message, $category = 'app') { logger()->debug($message, $category); } }