Use attrs on exception classes

This commit is contained in:
Mads Marquart
2019-12-11 16:20:38 +01:00
parent 91d4055545
commit a7b08fefe4
2 changed files with 22 additions and 20 deletions

View File

@@ -1,32 +1,32 @@
import attr
# Not frozen, since that doesn't work in PyPy
attrs_exception = attr.s(slots=True, auto_exc=True)
@attrs_exception
class FBchatException(Exception): class FBchatException(Exception):
"""Custom exception thrown by ``fbchat``. """Custom exception thrown by ``fbchat``.
All exceptions in the ``fbchat`` module inherits this. All exceptions in the ``fbchat`` module inherits this.
""" """
message = attr.ib()
@attrs_exception
class FBchatFacebookError(FBchatException): class FBchatFacebookError(FBchatException):
"""Raised when Facebook returns an error."""
#: The error code that Facebook returned #: The error code that Facebook returned
fb_error_code = None fb_error_code = attr.ib(None)
#: The error message that Facebook returned (In the user's own language) #: The error message that Facebook returned (In the user's own language)
fb_error_message = None fb_error_message = attr.ib(None)
#: The status code that was sent in the HTTP response (e.g. 404) (Usually only set if not successful, aka. not 200) #: The status code that was sent in the HTTP response (e.g. 404) (Usually only set if not successful, aka. not 200)
request_status_code = None request_status_code = attr.ib(None)
def __init__(
self,
message,
fb_error_code=None,
fb_error_message=None,
request_status_code=None,
):
super(FBchatFacebookError, self).__init__(message)
"""Thrown by ``fbchat`` when Facebook returns an error"""
self.fb_error_code = str(fb_error_code)
self.fb_error_message = fb_error_message
self.request_status_code = request_status_code
@attrs_exception
class FBchatInvalidParameters(FBchatFacebookError): class FBchatInvalidParameters(FBchatFacebookError):
"""Raised by Facebook if: """Raised by Facebook if:
@@ -36,17 +36,19 @@ class FBchatInvalidParameters(FBchatFacebookError):
""" """
@attrs_exception
class FBchatNotLoggedIn(FBchatFacebookError): class FBchatNotLoggedIn(FBchatFacebookError):
"""Raised by Facebook if the client has been logged out.""" """Raised by Facebook if the client has been logged out."""
fb_error_code = "1357001" fb_error_code = attr.ib("1357001")
@attrs_exception
class FBchatPleaseRefresh(FBchatFacebookError): class FBchatPleaseRefresh(FBchatFacebookError):
"""Raised by Facebook if the client has been inactive for too long. """Raised by Facebook if the client has been inactive for too long.
This error usually happens after 1-2 days of inactivity. This error usually happens after 1-2 days of inactivity.
""" """
fb_error_code = "1357004" fb_error_code = attr.ib("1357004")
fb_error_message = "Please try closing and re-opening your browser window." fb_error_message = attr.ib("Please try closing and re-opening your browser window.")

View File

@@ -14,7 +14,7 @@ maintainer-email = "madsmtm@gmail.com"
home-page = "https://github.com/carpedm20/fbchat/" home-page = "https://github.com/carpedm20/fbchat/"
requires = [ requires = [
"aenum~=2.0", "aenum~=2.0",
"attrs>=18.2", "attrs>=19.1",
"requests~=2.19", "requests~=2.19",
"beautifulsoup4~=4.0", "beautifulsoup4~=4.0",
] ]