From b5a37e35c620c92f47217b3bdf790ab06325f9ae Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 30 Aug 2019 18:06:05 +0200 Subject: [PATCH] Remove FBchatUserError in favor of builtin exceptions --- .github/ISSUE_TEMPLATE/bug_report.md | 4 ++-- docs/api.rst | 1 - fbchat/__init__.py | 2 +- fbchat/_client.py | 22 +++++++++++----------- fbchat/_exception.py | 4 ---- fbchat/_state.py | 2 +- tests/test_base.py | 4 ++-- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 40e7f5b..453d42c 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -21,8 +21,8 @@ Traceback (most recent call last): File "[site-packages]/fbchat/client.py", line 78, in __init__ self.login(email, password, max_tries) File "[site-packages]/fbchat/client.py", line 407, in login - raise FBchatUserError('Login failed. Check email/password. (Failed on URL: {})'.format(login_url)) -fbchat.models.FBchatUserError: Login failed. Check email/password. (Failed on URL: https://m.facebook.com/login.php?login_attempt=1) + raise FBchatException('Login failed. Check email/password. (Failed on URL: {})'.format(login_url)) +fbchat.FBchatException: Login failed. Check email/password. (Failed on URL: https://m.facebook.com/login.php?login_attempt=1) ``` ## Environment information diff --git a/docs/api.rst b/docs/api.rst index 3d9a920..83a9466 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -38,7 +38,6 @@ Exceptions .. autoexception:: FBchatException() .. autoexception:: FBchatFacebookError() -.. autoexception:: FBchatUserError() Attachments ----------- diff --git a/fbchat/__init__.py b/fbchat/__init__.py index 0b552fe..36d1659 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -11,7 +11,7 @@ _logging.getLogger(__name__).addHandler(_logging.NullHandler()) # The order of these is somewhat significant, e.g. User has to be imported after Thread! from . import _core, _util -from ._exception import FBchatException, FBchatFacebookError, FBchatUserError +from ._exception import FBchatException, FBchatFacebookError from ._thread import ThreadType, ThreadLocation, ThreadColor, Thread from ._user import TypingStatus, User, ActiveStatus from ._group import Group diff --git a/fbchat/_client.py b/fbchat/_client.py index a89edb9..d85e0d8 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -7,7 +7,7 @@ from collections import OrderedDict from ._core import log from . import _util, _graphql, _state -from ._exception import FBchatException, FBchatFacebookError, FBchatUserError +from ._exception import FBchatException, FBchatFacebookError from ._thread import ThreadType, ThreadLocation, ThreadColor from ._user import TypingStatus, User, ActiveStatus from ._group import Group @@ -176,10 +176,10 @@ class Client: self.onLoggingIn(email=email) if max_tries < 1: - raise FBchatUserError("Cannot login: max_tries should be at least one") + raise ValueError("Cannot login: max_tries should be at least one") if not (email and password): - raise FBchatUserError("Email and password not set") + raise ValueError("Email and password not set") for i in range(1, max_tries + 1): try: @@ -560,7 +560,7 @@ class Client: if thread.type == ThreadType.USER: users[id_] = thread else: - raise FBchatUserError("Thread {} was not a user".format(thread)) + raise ValueError("Thread {} was not a user".format(thread)) return users @@ -585,7 +585,7 @@ class Client: if thread.type == ThreadType.PAGE: pages[id_] = thread else: - raise FBchatUserError("Thread {} was not a page".format(thread)) + raise ValueError("Thread {} was not a page".format(thread)) return pages @@ -607,7 +607,7 @@ class Client: if thread.type == ThreadType.GROUP: groups[id_] = thread else: - raise FBchatUserError("Thread {} was not a group".format(thread)) + raise ValueError("Thread {} was not a group".format(thread)) return groups @@ -739,12 +739,12 @@ class Client: FBchatException: If request failed """ if limit > 20 or limit < 1: - raise FBchatUserError("`limit` should be between 1 and 20") + raise ValueError("`limit` should be between 1 and 20") if thread_location in ThreadLocation: loc_str = thread_location.value else: - raise FBchatUserError('"thread_location" must be a value of ThreadLocation') + raise TypeError('"thread_location" must be a value of ThreadLocation') params = { "limit": limit, @@ -1033,7 +1033,7 @@ class Client: ) elif isinstance(quick_reply, QuickReplyLocation): if not isinstance(payload, LocationAttachment): - raise ValueError( + raise TypeError( "Payload must be an instance of `fbchat.LocationAttachment`" ) return self.sendLocation( @@ -1274,7 +1274,7 @@ class Client: data = self._oldMessage(message)._to_send_data() if len(user_ids) < 2: - raise FBchatUserError("Error when creating group: Not enough participants") + raise ValueError("Error when creating group: Not enough participants") for i, user_id in enumerate(user_ids + [self._uid]): data["specific_to_list[{}]".format(i)] = "fbid:{}".format(user_id) @@ -1305,7 +1305,7 @@ class Client: for i, user_id in enumerate(user_ids): if user_id == self._uid: - raise FBchatUserError( + raise ValueError( "Error when adding users: Cannot add self to group thread" ) else: diff --git a/fbchat/_exception.py b/fbchat/_exception.py index c28d68b..b84c5b9 100644 --- a/fbchat/_exception.py +++ b/fbchat/_exception.py @@ -50,7 +50,3 @@ class FBchatPleaseRefresh(FBchatFacebookError): fb_error_code = "1357004" fb_error_message = "Please try closing and re-opening your browser window." - - -class FBchatUserError(FBchatException): - """Thrown by ``fbchat`` when wrong values are entered.""" diff --git a/fbchat/_state.py b/fbchat/_state.py index eb9caa1..f8b489a 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -147,7 +147,7 @@ class State: if is_home(r.url): return cls.from_session(session=session) else: - raise _exception.FBchatUserError( + raise _exception.FBchatException( "Login failed. Check email/password. " "(Failed on url: {})".format(r.url) ) diff --git a/tests/test_base.py b/tests/test_base.py index 1528968..6cdf43a 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -3,7 +3,7 @@ import py_compile from glob import glob from os import path, environ -from fbchat import FBchatUserError, Message, Client +from fbchat import FBchatException, Message, Client @pytest.mark.offline @@ -24,7 +24,7 @@ def test_login(client1): assert not client1.isLoggedIn() - with pytest.raises(FBchatUserError): + with pytest.raises(FBchatException): client1.login("", "", max_tries=1) client1.login(email, password)