service = Service::getInstance(); } /** * 处理系统错误 * * @param TcpConnection $connection 连接实例 * @param \Throwable $e 异常实例 * @param array $context 上下文 * @return void */ protected function handleSysError(TcpConnection $connection, \Throwable $e, array $context = []): void { $this->logError('System error', array_merge([ 'connectionId' => $connection->id, 'deviceId' => $connection->deviceId ?? '', 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ], $context)); } /** * 处理普通错误 * * @param \Throwable $e 异常实例 * @param array $context 上下文 * @return void */ protected function handleNormalError(\Throwable $e, array $context = []): void { $this->logError('Normal error', array_merge([ 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ], $context)); } /** * 记录错误日志 * * @param string $message 消息 * @param array $context 上下文 * @return void */ protected function logError(string $message, array $context = []): void { Log::error($message, $context); } /** * 记录警告日志 * * @param string $message 消息 * @param array $context 上下文 * @return void */ protected function logWarning(string $message, array $context = []): void { Log::warning($message, $context); } /** * 记录信息日志 * * @param string $message 消息 * @param array $context 上下文 * @return void */ protected function logInfo(string $message, array $context = []): void { Log::info($message, $context); } /** * 记录调试日志 * * @param string $message 消息 * @param array $context 上下文 * @return void */ protected function logDebug(string $message, array $context = []): void { Log::debug($message, $context); } /** * 组装响应 * * @param int $code 响应码 * @param string $message 响应消息 * @param string $msgType 消息类型 * @param array $data 响应数据 * @return array */ protected function buildResponse(int $code = ResponseCode::SUCCESS, string $message = 'Success', string $msgType = '', array $data = []): array { // 组装响应 $response = [ "Code" => $code, "Message" => $message, "Data" => [ "MsgType" => $msgType, "Content" => $data ] ]; return $response; } }