Skip to content

Commit

Permalink
Fix request error handling (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX committed May 24, 2023
1 parent 3ad1569 commit 111c292
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
22 changes: 16 additions & 6 deletions atproto/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import typing as t

if t.TYPE_CHECKING:
from atproto.xrpc_client.request import Response


class AtProtocolError(Exception):
"""Base exception"""

Expand Down Expand Up @@ -42,24 +48,28 @@ class ModelFieldNotFoundError(ModelError):
...


class NetworkError(AtProtocolError):
class RequestErrorBase(AtProtocolError):
def __init__(self, response: t.Optional['Response'] = None):
self.response: 'Response' = response


class NetworkError(RequestErrorBase):
...


class InvokeTimeoutError(NetworkError):
...


class UnauthorizedError(AtProtocolError):
def __init__(self, response):
self.response = response
class UnauthorizedError(RequestErrorBase):
...


class RequestException(AtProtocolError):
class RequestException(RequestErrorBase):
...


class BadRequestError(RequestException):
class BadRequestError(RequestErrorBase):
...


Expand Down
2 changes: 1 addition & 1 deletion atproto/xrpc_client/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _handle_response(response: httpx.Response) -> httpx.Response:

if response.status_code in {401, 403}:
raise exceptions.UnauthorizedError(error_response)
elif response.status_code == 404:
elif response.status_code == 400:
raise exceptions.BadRequestError(error_response)
elif response.status_code in {409, 413, 502}:
raise exceptions.NetworkError(error_response)
Expand Down
20 changes: 12 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import threading

from atproto import CAR, AsyncClient, AtUri, Client, models
from atproto import CAR, AsyncClient, AtUri, Client, exceptions, models
from atproto.firehose import (
AsyncFirehoseSubscribeLabelsClient,
AsyncFirehoseSubscribeReposClient,
Expand Down Expand Up @@ -40,7 +40,7 @@ def sync_main():
client.login(os.environ['USERNAME'], os.environ['PASSWORD'])

# repo = client.com.atproto.sync.get_repo({'did': client.me.did})
did = client.com.atproto.identity.resolve_handle({'handle': 'bsky.app'}).did
# did = client.com.atproto.identity.resolve_handle({'handle': 'bsky.app'}).did
# repo = client.com.atproto.sync.get_repo({'did': did})
# car_file = CAR.from_bytes(repo)
# print(car_file.root)
Expand All @@ -62,10 +62,14 @@ def sync_main():

# client.com.atproto.repo.get_record({'collection': 'app.bsky.feed.post', 'repo': 'arta.bsky.social'})

# with open('cat2.jpg', 'rb') as f:
# cat_data = f.read()
#
# client.send_image('Cat looking for a Python', cat_data, 'cat alt')
with open('cat_big.png', 'rb') as f:
cat_data = f.read()
try:
client.send_image('Cat looking for a Python', cat_data, 'cat alt')
except exceptions.BadRequestError as e:
print('Status code:', e.response.status_code)
print('Error code:', e.response.content.error)
print('Error message:', e.response.content.message)
#
# resolve = client.com.atproto.identity.resolve_handle(models.ComAtprotoIdentityResolveHandle.Params(profile.handle))
# assert resolve.did == profile.did
Expand Down Expand Up @@ -180,8 +184,8 @@ async def _stop_after_n_sec():
if __name__ == '__main__':
# test_strange_embed_images_type()

# sync_main()
sync_main()
# asyncio.get_event_loop().run_until_complete(main())

# _main_firehose_test()
asyncio.get_event_loop().run_until_complete(_main_async_firehose_test())
# asyncio.get_event_loop().run_until_complete(_main_async_firehose_test())

0 comments on commit 111c292

Please sign in to comment.