mirror of
https://github.com/lilydjwg/nvchecker
synced 2025-02-05 04:51:54 +00:00
Add exception HTTPError for HTTP 4xx errors
This commit is contained in:
parent
f957acc756
commit
8d83d7ac66
@ -1,7 +1,7 @@
|
||||
# MIT licensed
|
||||
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
||||
|
||||
from .httpclient import session, TemporaryError
|
||||
from .httpclient import session, TemporaryError, HTTPError
|
||||
from .util import (
|
||||
Entry, BaseWorker, RawResult, VersionResult,
|
||||
AsyncCache, KeyManager, GetVersionError,
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from .base import TemporaryError
|
||||
from .base import TemporaryError, HTTPError
|
||||
|
||||
class Proxy:
|
||||
_obj = None
|
||||
|
@ -8,7 +8,7 @@ from typing import Optional, Dict
|
||||
import structlog
|
||||
import aiohttp
|
||||
|
||||
from .base import BaseSession, TemporaryError, Response
|
||||
from .base import BaseSession, TemporaryError, Response, HTTPError
|
||||
|
||||
__all__ = ['session']
|
||||
|
||||
@ -54,10 +54,13 @@ class AiohttpSession(BaseSession):
|
||||
) as e:
|
||||
raise TemporaryError(599, repr(e), e)
|
||||
|
||||
err_cls: Optional[type] = None
|
||||
if res.status >= 500:
|
||||
raise TemporaryError(res.status, res.reason, res)
|
||||
else:
|
||||
res.raise_for_status()
|
||||
err_cls = TemporaryError
|
||||
elif res.status >= 400:
|
||||
err_cls = HTTPError
|
||||
if err_cls is not None:
|
||||
raise err_cls(res.status, res.reason, res)
|
||||
|
||||
body = await res.content.read()
|
||||
return Response(body)
|
||||
|
@ -86,10 +86,14 @@ class BaseSession:
|
||||
''':meta private:'''
|
||||
raise NotImplementedError
|
||||
|
||||
class TemporaryError(Exception):
|
||||
'''A temporary error (e.g. network error) happens.'''
|
||||
class BaseHTTPError(Exception):
|
||||
def __init__(self, code, message, response):
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.response = response
|
||||
|
||||
class TemporaryError(BaseHTTPError):
|
||||
'''A temporary error (e.g. network error) happens.'''
|
||||
|
||||
class HTTPError(BaseHTTPError):
|
||||
''' An HTTP 4xx error happens '''
|
||||
|
@ -6,7 +6,7 @@ from typing import Dict, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from .base import BaseSession, TemporaryError, Response
|
||||
from .base import BaseSession, TemporaryError, Response, HTTPError
|
||||
|
||||
__all__ = ['session']
|
||||
|
||||
@ -42,14 +42,17 @@ class HttpxSession(BaseSession):
|
||||
headers = headers,
|
||||
params = params,
|
||||
)
|
||||
err_cls: Optional[type] = None
|
||||
if r.status_code >= 500:
|
||||
raise TemporaryError(
|
||||
err_cls = TemporaryError
|
||||
elif r.status_code >= 400:
|
||||
err_cls = HTTPError
|
||||
if err_cls is not None:
|
||||
raise err_cls(
|
||||
r.status_code,
|
||||
r.reason_phrase,
|
||||
r,
|
||||
)
|
||||
else:
|
||||
r.raise_for_status()
|
||||
|
||||
except httpx.TransportError as e:
|
||||
raise TemporaryError(599, repr(e), e)
|
||||
|
@ -12,7 +12,7 @@ try:
|
||||
except ImportError:
|
||||
pycurl = None # type: ignore
|
||||
|
||||
from .base import BaseSession, TemporaryError, Response
|
||||
from .base import BaseSession, TemporaryError, Response, HTTPError
|
||||
|
||||
__all__ = ['session']
|
||||
|
||||
@ -74,12 +74,15 @@ class TornadoSession(BaseSession):
|
||||
r = HTTPRequest(url, **kwargs)
|
||||
res = await AsyncHTTPClient().fetch(
|
||||
r, raise_error=False)
|
||||
err_cls: Optional[type] = None
|
||||
if res.code >= 500:
|
||||
raise TemporaryError(
|
||||
err_cls = TemporaryError
|
||||
elif res.code >= 400:
|
||||
err_cls = HTTPError
|
||||
if err_cls is not None:
|
||||
raise err_cls(
|
||||
res.code, res.reason, res
|
||||
)
|
||||
else:
|
||||
res.rethrow()
|
||||
|
||||
return Response(res.body)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user