diff --git a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py index ba63c85f..933bd0e3 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py @@ -282,8 +282,8 @@ class AutoCURLHandle(CURL): class CURLException(Exception): def __init__(self,code) -> None: - self.errorcode=code.value if isinstance(code,CURLcode): + self.errorcode=code.value error=curl_easy_strerror(code).decode('utf8') for _ in dir(CURLcode): if _.startswith('CURLE_') and code.value==getattr(CURLcode,_): diff --git a/LunaTranslator/LunaTranslator/network/libcurl/requests.py b/LunaTranslator/LunaTranslator/network/libcurl/requests.py index 2e570a8e..f30f507b 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/requests.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/requests.py @@ -19,6 +19,17 @@ class Response(ResponseBase): def raise_for_status(self): if self.last_error: raise CURLException(self.last_error) +def ExceptionFilter(func): + def _wrapper(*args,**kwargs): + try: + _= func(*args,**kwargs) + return _ + except CURLException as e: + if e.errorcode==CURLcode.CURLE_OPERATION_TIMEDOUT: + raise Timeout(e) + else: + raise e + return _wrapper class Session(Sessionbase): def __init__(self) -> None: @@ -57,18 +68,9 @@ class Session(Sessionbase): def _getmembyte(self,mem): return cast(mem.memory,POINTER(c_char))[:mem.size] + @ExceptionFilter def request_impl(self, method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout): - try: - _= self.request_impl_1(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout) - return _ - except CURLException as e: - if e.errorcode==CURLcode.CURLE_OPERATION_TIMEDOUT: - raise Timeout(e) - else: - raise e - def request_impl_1(self, - method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout): if self._status==0: curl=self.curl diff --git a/LunaTranslator/LunaTranslator/network/winhttp/requests.py b/LunaTranslator/LunaTranslator/network/winhttp/requests.py index add839b0..a08f199a 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/requests.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/requests.py @@ -23,6 +23,18 @@ class Response(ResponseBase): error=GetLastError() if error: raise WinhttpException(error) +def ExceptionFilter(func): + def _wrapper(*args,**kwargs): + try: + _= func(*args,**kwargs) + return _ + except WinhttpException as e: + if e.errorcode==WinhttpException.ERROR_WINHTTP_TIMEOUT: + raise Timeout(e) + else: + raise e + return _wrapper + class Session(Sessionbase): def __init__(self) -> None: super().__init__() @@ -60,17 +72,8 @@ class Session(Sessionbase): if verify==False: dwFlags=DWORD(SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS) WinHttpSetOption(curl,WINHTTP_OPTION_SECURITY_FLAGS, pointer(dwFlags),sizeof(dwFlags)) + @ExceptionFilter def request_impl(self, - method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout): - try: - _= self.request_impl_1(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout) - return _ - except WinhttpException as e: - if e.errorcode==WinhttpException.ERROR_WINHTTP_TIMEOUT: - raise Timeout(e) - else: - raise e - def request_impl_1(self, method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout): headers=self._parseheader(headers,cookies) flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0