-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/handle request errors (#150)
* feat: handle response body and connect errors * test: botx method with empty error handlers * docs: add release changes
- Loading branch information
Showing
14 changed files
with
333 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Definition for "bot not found" error.""" | ||
from typing import NoReturn | ||
|
||
from botx.clients.methods.base import APIErrorResponse, BotXMethod | ||
from botx.clients.types.http import HTTPResponse | ||
from botx.exceptions import BotXAPIError | ||
|
||
|
||
class BotNotFoundError(BotXAPIError): | ||
"""Error for raising when bot not found.""" | ||
|
||
message_template = "bot with id `{bot_id}` not found. " | ||
|
||
|
||
def handle_error(method: BotXMethod, response: HTTPResponse) -> NoReturn: | ||
"""Handle "bot not found" error response. | ||
Arguments: | ||
method: method which was made before error. | ||
response: HTTP response from BotX API. | ||
Raises: | ||
BotNotFoundError: raised always. | ||
""" | ||
APIErrorResponse[dict].parse_obj(response.json_body) | ||
raise BotNotFoundError( | ||
url=method.url, | ||
method=method.http_method, | ||
response_content=response.json_body, | ||
status_content=response.status_code, | ||
bot_id=method.bot_id, # type: ignore | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Definition for "invalid bot credentials" error.""" | ||
from typing import NoReturn | ||
|
||
from botx.clients.methods.base import APIErrorResponse, BotXMethod | ||
from botx.clients.types.http import HTTPResponse | ||
from botx.exceptions import BotXAPIError | ||
|
||
|
||
class InvalidBotCredentials(BotXAPIError): | ||
"""Error for raising when got invalid bot credentials.""" | ||
|
||
message_template = ( | ||
"Can't get token for bot {bot_id}. Make sure bot credentials is correct" | ||
) | ||
|
||
|
||
def handle_error(method: BotXMethod, response: HTTPResponse) -> NoReturn: | ||
"""Handle "invalid bot credentials" error response. | ||
Arguments: | ||
method: method which was made before error. | ||
response: HTTP response from BotX API. | ||
Raises: | ||
InvalidBotCredentials: raised always. | ||
""" | ||
APIErrorResponse[dict].parse_obj(response.json_body) | ||
raise InvalidBotCredentials( | ||
url=method.url, | ||
method=method.http_method, | ||
response_content=response.json_body, | ||
status_content=response.status_code, | ||
bot_id=method.bot_id, # type: ignore | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
51 changes: 51 additions & 0 deletions
51
tests/test_clients/test_clients/test_async_client/test_execute.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import uuid | ||
|
||
import pytest | ||
from httpx import ConnectError, Request, Response | ||
|
||
from botx.clients.methods.v2.bots.token import Token | ||
from botx.exceptions import BotXConnectError, BotXJSONDecodeError | ||
|
||
try: | ||
from unittest.mock import AsyncMock | ||
except ImportError: | ||
from unittest.mock import MagicMock | ||
|
||
# Used for compatibility with python 3.7 | ||
class AsyncMock(MagicMock): | ||
async def __call__(self, *args, **kwargs): | ||
return super(AsyncMock, self).__call__(*args, **kwargs) | ||
|
||
|
||
@pytest.fixture() | ||
def token_method(): | ||
return Token(host="example.cts", bot_id=uuid.uuid4(), signature="signature") | ||
|
||
|
||
@pytest.fixture() | ||
def mock_http_client(): | ||
return AsyncMock() | ||
|
||
|
||
@pytest.mark.asyncio() | ||
async def test_raising_connection_error(client, token_method, mock_http_client): | ||
request = Request(token_method.http_method, token_method.url) | ||
mock_http_client.request.side_effect = ConnectError("Test error", request=request) | ||
|
||
client.bot.client.http_client = mock_http_client | ||
botx_request = client.bot.client.build_request(token_method) | ||
|
||
with pytest.raises(BotXConnectError): | ||
await client.bot.client.execute(botx_request) | ||
|
||
|
||
@pytest.mark.asyncio() | ||
async def test_raising_decode_error(client, token_method, mock_http_client): | ||
response = Response(status_code=418, text="Wrong json") | ||
mock_http_client.request.return_value = response | ||
|
||
client.bot.client.http_client = mock_http_client | ||
botx_request = client.bot.client.build_request(token_method) | ||
|
||
with pytest.raises(BotXJSONDecodeError): | ||
await client.bot.client.execute(botx_request) |
42 changes: 39 additions & 3 deletions
42
tests/test_clients/test_clients/test_sync_client/test_execute.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,46 @@ | ||
import uuid | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from httpx import ConnectError, Request, Response | ||
|
||
from botx.clients.methods.v2.bots.token import Token | ||
from botx.exceptions import BotXConnectError, BotXJSONDecodeError | ||
|
||
|
||
@pytest.fixture() | ||
def token_method(): | ||
return Token(host="example.cts", bot_id=uuid.uuid4(), signature="signature") | ||
|
||
|
||
@pytest.fixture() | ||
def mock_http_client(): | ||
return Mock() | ||
|
||
def test_execute_without_explicit_host(client): | ||
method = Token(host="example.cts", bot_id=uuid.uuid4(), signature="signature") | ||
request = client.bot.sync_client.build_request(method) | ||
|
||
def test_execute_without_explicit_host(client, token_method): | ||
request = client.bot.sync_client.build_request(token_method) | ||
|
||
assert client.bot.sync_client.execute(request) | ||
|
||
|
||
def test_raising_connection_error(client, token_method, mock_http_client): | ||
request = Request(token_method.http_method, token_method.url) | ||
mock_http_client.request.side_effect = ConnectError("Test error", request=request) | ||
|
||
client.bot.sync_client.http_client = mock_http_client | ||
botx_request = client.bot.sync_client.build_request(token_method) | ||
|
||
with pytest.raises(BotXConnectError): | ||
client.bot.sync_client.execute(botx_request) | ||
|
||
|
||
def test_raising_decode_error(client, token_method, mock_http_client): | ||
response = Response(status_code=418, text="Wrong json") | ||
mock_http_client.request.return_value = response | ||
|
||
client.bot.sync_client.http_client = mock_http_client | ||
botx_request = client.bot.sync_client.build_request(token_method) | ||
|
||
with pytest.raises(BotXJSONDecodeError): | ||
client.bot.sync_client.execute(botx_request) |
13 changes: 13 additions & 0 deletions
13
tests/test_clients/test_methods/test_base/test_empty_error_handlers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from botx.clients.methods.base import BotXMethod | ||
|
||
|
||
class TestMethod(BotXMethod): | ||
__url__ = "/path/to/example" | ||
__method__ = "GET" | ||
__returning__ = str | ||
|
||
|
||
def test_method_empty_error_handlers(): | ||
test_method = TestMethod() | ||
|
||
assert test_method.__errors_handlers__ == {} |
Oops, something went wrong.
b3843ad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://60f5a0a1bf06fe3cf7356832--pybotx.netlify.app