Skip to content

Commit a4329b4

Browse files
committed
fix: actualize error responses for thread creation
1 parent cfdf8bd commit a4329b4

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

pybotx/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
ChatCreationError,
4040
ChatCreationProhibitedError,
4141
InvalidUsersListError,
42+
ThreadAlreadyExistsError,
4243
ThreadCreationError,
4344
ThreadCreationProhibitedError,
4445
)
@@ -264,8 +265,8 @@
264265
"SyncSmartAppEventHandlerFunc",
265266
"SyncSmartAppEventHandlerNotFoundError",
266267
"SyncSourceTypes",
268+
"ThreadAlreadyExistsError",
267269
"ThreadCreationError",
268-
"ThreadCreationEventNotFoundError",
269270
"ThreadCreationProhibitedError",
270271
"UnknownBotAccountError",
271272
"UnknownSystemEventError",

pybotx/client/chats_api/create_thread.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from typing import Literal
1+
from typing import Literal, NoReturn
22
from uuid import UUID
33

4+
import httpx
5+
46
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
57
from pybotx.client.botx_method import response_exception_thrower
68
from pybotx.client.exceptions.chats import (
9+
ThreadAlreadyExistsError,
710
ThreadCreationError,
811
ThreadCreationProhibitedError,
912
)
@@ -31,12 +34,21 @@ def to_domain(self) -> UUID:
3134
return self.result.thread_id
3235

3336

37+
def conflict_error_handler(response: httpx.Response) -> NoReturn:
38+
reason = response.json().get("reason")
39+
40+
if reason == "thread_already_created":
41+
raise ThreadAlreadyExistsError.from_response(response)
42+
43+
raise ThreadCreationError.from_response(response)
44+
45+
3446
class CreateThreadMethod(AuthorizedBotXMethod):
3547
status_handlers = {
3648
**AuthorizedBotXMethod.status_handlers,
3749
403: response_exception_thrower(ThreadCreationProhibitedError),
3850
404: response_exception_thrower(EventNotFoundError),
39-
422: response_exception_thrower(ThreadCreationError),
51+
409: conflict_error_handler,
4052
}
4153

4254
async def execute(

pybotx/client/exceptions/chats.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ class ChatCreationError(BaseClientError):
1717
"""Error while chat creation."""
1818

1919

20+
class ThreadAlreadyExistsError(BaseClientError):
21+
"""Thread is already exists."""
22+
23+
2024
class ThreadCreationError(BaseClientError):
21-
"""Error while thread creation (invalid scheme)."""
25+
"""Creating a thread for a deleted message."""
2226

2327

2428
class ThreadCreationProhibitedError(BaseClientError):
2529
"""
2630
Error while permission checks.
2731
2832
1. Bot has no permissions to create thread
29-
2. Threads are not allowed for that message
33+
2. Threads are not allowed for this chat
3034
3. Bot is not a chat member where message is located
3135
4. Message is located in personal chat
3236
5. Usupported event type
3337
6. Unsuppoerted chat type
34-
7. Thread is already created
35-
8. No access for message
36-
9. Message in stealth mode
37-
10. Message is deleted
38+
7. No access for message
39+
8. Message in stealth mode
3840
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pybotx"
3-
version = "0.75.4"
3+
version = "0.75.5"
44
description = "A python library for interacting with eXpress BotX API"
55
authors = [
66
"Sidnev Nikolay <[email protected]>",

tests/client/chats_api/test_create_thread.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from collections.abc import Callable
12
from http import HTTPStatus
2-
from typing import Any, Callable
3+
from typing import Any
34
from uuid import UUID
45

56
import httpx
@@ -12,6 +13,7 @@
1213
BotAccountWithSecret,
1314
EventNotFoundError,
1415
HandlerCollector,
16+
ThreadAlreadyExistsError,
1517
ThreadCreationError,
1618
ThreadCreationProhibitedError,
1719
lifespan_wrapper,
@@ -57,11 +59,10 @@ async def test__create_thread__succeed(
5759
bot_account: BotAccountWithSecret,
5860
) -> None:
5961
# - Arrange -
60-
thread_id = "2a8c0d1e-c4d1-4308-b024-6e1a9f4a4b6d"
6162
endpoint = create_mocked_endpoint(
6263
{
6364
"status": "ok",
64-
"result": {"thread_id": thread_id},
65+
"result": {"thread_id": sync_id},
6566
},
6667
HTTPStatus.OK,
6768
)
@@ -76,7 +77,7 @@ async def test__create_thread__succeed(
7677
)
7778

7879
# - Assert -
79-
assert str(created_thread_id) == thread_id
80+
assert str(created_thread_id) == sync_id
8081
assert endpoint.called
8182

8283

@@ -143,16 +144,6 @@ async def test__create_thread__succeed(
143144
HTTPStatus.FORBIDDEN,
144145
ThreadCreationProhibitedError,
145146
),
146-
(
147-
{
148-
"status": "error",
149-
"reason": "thread_already_created",
150-
"errors": ["Thread already created"],
151-
"error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"},
152-
},
153-
HTTPStatus.FORBIDDEN,
154-
ThreadCreationProhibitedError,
155-
),
156147
(
157148
{
158149
"status": "error",
@@ -176,32 +167,32 @@ async def test__create_thread__succeed(
176167
(
177168
{
178169
"status": "error",
179-
"reason": "event_already_deleted",
180-
"errors": ["This event already deleted"],
170+
"reason": "event_not_found",
171+
"errors": ["Event not found"],
181172
"error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"},
182173
},
183-
HTTPStatus.FORBIDDEN,
184-
ThreadCreationProhibitedError,
174+
HTTPStatus.NOT_FOUND,
175+
EventNotFoundError,
185176
),
186177
(
187178
{
188179
"status": "error",
189-
"reason": "event_not_found",
190-
"errors": ["Event not found"],
180+
"reason": "event_already_deleted",
181+
"errors": ["This event already deleted"],
191182
"error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"},
192183
},
193-
HTTPStatus.NOT_FOUND,
194-
EventNotFoundError,
184+
HTTPStatus.CONFLICT,
185+
ThreadCreationError,
195186
),
196187
(
197188
{
198189
"status": "error",
199-
"reason": "|specified reason|",
200-
"errors": ["|specified errors|"],
201-
"error_data": {},
190+
"reason": "thread_already_created",
191+
"errors": ["Thread already created"],
192+
"error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"},
202193
},
203-
HTTPStatus.UNPROCESSABLE_ENTITY,
204-
ThreadCreationError,
194+
HTTPStatus.CONFLICT,
195+
ThreadAlreadyExistsError,
205196
),
206197
(
207198
{

0 commit comments

Comments
 (0)