Skip to content

Commit

Permalink
Handle unexpected errors in HTTP requests (bfxapi.rest._interface).
Browse files Browse the repository at this point in the history
  • Loading branch information
Davi0kProgramsThings committed Apr 4, 2024
1 parent bdd78a8 commit 65318be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions bfxapi/rest/_interface/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import json
from datetime import datetime
from enum import IntEnum
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, List, NoReturn, Optional

import requests

from bfxapi._utils.json_decoder import JSONDecoder
from bfxapi._utils.json_encoder import JSONEncoder
from bfxapi.exceptions import InvalidCredentialError
from bfxapi.rest.exceptions import RequestParametersError, UnknownGenericError
from bfxapi.rest.exceptions import GenericError, RequestParameterError

if TYPE_CHECKING:
from requests.sessions import _Params
Expand Down Expand Up @@ -86,28 +86,30 @@ def post(

return data

def __handle_error(self, error: List[Any]) -> None:
def __handle_error(self, error: List[Any]) -> NoReturn:
if error[1] == _Error.ERR_PARAMS:
raise RequestParametersError(
"The request was rejected with the following parameter"
f"error: <{error[2]}>"
raise RequestParameterError(
"The request was rejected with the following parameter "
f"error: <{error[2]}>."
)

if error[1] == _Error.ERR_AUTH_FAIL:
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:
raise UnknownGenericError(
"The server replied to the request with a generic error with "
f"the following message: <{error[2]}>."
raise GenericError(
"The request was rejected with the following generic "
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):
assert (
self.__api_key and self.__api_secret
), "API-KEY and API-SECRET must be strings."
assert self.__api_key and self.__api_secret

nonce = str(round(datetime.now().timestamp() * 1_000_000))

Expand Down
4 changes: 2 additions & 2 deletions bfxapi/rest/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from bfxapi.exceptions import BfxBaseException


class RequestParametersError(BfxBaseException):
class RequestParameterError(BfxBaseException):
pass


class UnknownGenericError(BfxBaseException):
class GenericError(BfxBaseException):
pass

0 comments on commit 65318be

Please sign in to comment.