mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-18 14:24:21 +01:00
Handle unexpected errors in HTTP requests (bfxapi.rest._interface).
This commit is contained in:
@@ -3,14 +3,14 @@ import hmac
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from typing import TYPE_CHECKING, Any, List, Optional
|
from typing import TYPE_CHECKING, Any, List, NoReturn, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from bfxapi._utils.json_decoder import JSONDecoder
|
from bfxapi._utils.json_decoder import JSONDecoder
|
||||||
from bfxapi._utils.json_encoder import JSONEncoder
|
from bfxapi._utils.json_encoder import JSONEncoder
|
||||||
from bfxapi.exceptions import InvalidCredentialError
|
from bfxapi.exceptions import InvalidCredentialError
|
||||||
from bfxapi.rest.exceptions import RequestParametersError, UnknownGenericError
|
from bfxapi.rest.exceptions import GenericError, RequestParameterError
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from requests.sessions import _Params
|
from requests.sessions import _Params
|
||||||
@@ -86,28 +86,30 @@ class Middleware:
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def __handle_error(self, error: List[Any]) -> None:
|
def __handle_error(self, error: List[Any]) -> NoReturn:
|
||||||
if error[1] == _Error.ERR_PARAMS:
|
if error[1] == _Error.ERR_PARAMS:
|
||||||
raise RequestParametersError(
|
raise RequestParameterError(
|
||||||
"The request was rejected with the following parameter"
|
"The request was rejected with the following parameter "
|
||||||
f"error: <{error[2]}>"
|
f"error: <{error[2]}>."
|
||||||
)
|
)
|
||||||
|
|
||||||
if error[1] == _Error.ERR_AUTH_FAIL:
|
if error[1] == _Error.ERR_AUTH_FAIL:
|
||||||
raise InvalidCredentialError(
|
raise InvalidCredentialError(
|
||||||
"Cannot authenticate with given API-KEY and API-SECRET."
|
"Can't authenticate with given API-KEY and API-SECRET."
|
||||||
)
|
)
|
||||||
|
|
||||||
if not error[1] or error[1] == _Error.ERR_UNK or error[1] == _Error.ERR_GENERIC:
|
if not error[1] or error[1] == _Error.ERR_UNK or error[1] == _Error.ERR_GENERIC:
|
||||||
raise UnknownGenericError(
|
raise GenericError(
|
||||||
"The server replied to the request with a generic error with "
|
"The request was rejected with the following generic "
|
||||||
f"the following message: <{error[2]}>."
|
f"error: <{error[2]}>."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
f"The request was rejected with an unexpected error: <{error}>."
|
||||||
|
)
|
||||||
|
|
||||||
def __get_authentication_headers(self, endpoint: str, data: Optional[str] = None):
|
def __get_authentication_headers(self, endpoint: str, data: Optional[str] = None):
|
||||||
assert (
|
assert self.__api_key and self.__api_secret
|
||||||
self.__api_key and self.__api_secret
|
|
||||||
), "API-KEY and API-SECRET must be strings."
|
|
||||||
|
|
||||||
nonce = str(round(datetime.now().timestamp() * 1_000_000))
|
nonce = str(round(datetime.now().timestamp() * 1_000_000))
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from bfxapi.exceptions import BfxBaseException
|
from bfxapi.exceptions import BfxBaseException
|
||||||
|
|
||||||
|
|
||||||
class RequestParametersError(BfxBaseException):
|
class RequestParameterError(BfxBaseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class UnknownGenericError(BfxBaseException):
|
class GenericError(BfxBaseException):
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user