コード例 #1
0
 def callback(self, worker):
     worker_exception = worker.exception()
     if worker_exception:
         try:
             raise worker_exception
         except Exception:
             logger.error(traceback.format_exc())
コード例 #2
0
ファイル: async_client.py プロジェクト: jihuoma/botoy
 async def run(self):
     try:
         await self.socketio.connect(self.config.address,
                                     transports=['websocket'])
     except Exception:
         logger.error(traceback.format_exc())
         await self.close()
     else:
         try:
             await self.socketio.wait()
         finally:
             print('bye~')
             await self.close()
コード例 #3
0
 def run(self):
     logger.info('Connecting to the server...')
     try:
         self.socketio.connect(self.config.address, transports=['websocket'])
     except Exception:
         logger.error(traceback.format_exc())
         self.close(1)
     else:
         try:
             self.socketio.wait()
         except KeyboardInterrupt:
             pass
         finally:
             print('bye~')
             self.close(0)
コード例 #4
0
ファイル: async_action.py プロジェクト: milkice233/botoy
    async def baseRequest(
        self,
        method: str,
        funcname: str,
        path: str,
        payload: dict = None,
        params: dict = None,
    ) -> dict:
        """基础请求方法, 提供部分提示信息,出错返回空字典,其他返回服务端响应结果"""
        if params is not None:
            params = params.update({'funcname': funcname})
        else:
            params = {'funcname': funcname}

        # 发送请求
        try:
            resp = await self.c.request(
                method, httpx.URL(url=path, params=params), json=payload
            )
            resp.raise_for_status()
        except httpx.HTTPError as e:
            if isinstance(e, httpx.TimeoutException):
                logger.warning(f'响应超时,但不代表处理未成功, 结果未知! => {e}')
            elif isinstance(e, httpx.HTTPStatusError):
                logger.error(
                    f"响应码出错 => {resp.status_code}",
                )
            else:
                logger.error(f'请求出错: {traceback.format_exc()}')
            return {}

        # 处理数据
        try:
            data = resp.json()
        except json.JSONDecodeError as e:
            logger.error('API响应结果非json格式')
            return {}

        if data is None:
            logger.error('返回为null')
            return {}

        # 返回码提示
        if 'Ret' in data:
            ret = data.get('Ret')
            if ret == 0:
                pass
            elif ret == 34:
                logger.error(f'未知错误,跟消息长度似乎无关,可以尝试分段重新发送 => {data}')
            elif ret == 110:
                logger.error(f'发送失败,你已被移出该群,请重新加群 => {data}')
            elif ret == 120:
                logger.error(f'机器人被禁言 => {data}')
            elif ret == 241:
                logger.error(f'消息发送频率过高,对同一个群或好友,建议发消息的最小间隔控制在1100ms以上 => {data}')
            elif ret == 299:
                logger.error(f'超过群发言频率限制 => {data}')
            else:
                logger.error(f'请求发送成功, 但处理失败 => {data}')

        return data