Browse Source

Defined the __str__ method for all exceptions/warnings. Next step is to

show it when we get those.
master
Julio Biason 15 years ago
parent
commit
b927b0cff4
  1. 13
      mitterlib/network/__init__.py
  2. 53
      mitterlib/network/networkbase.py
  3. 10
      mitterlib/network/twitter.py

13
mitterlib/network/__init__.py

@ -20,6 +20,7 @@
import logging
import os.path
import urllib2
import gettext
from mitterlib.network.networkbase import NetworkData
from mitterlib import module_search
@ -29,6 +30,13 @@ _log = logging.getLogger('mitterlib.network.Networks')
# List of files that are not networks
SKIPPABLES = ('__init__.py', 'networkbase.py')
#--------------------------------------------------------------------
# i18n
#--------------------------------------------------------------------
t = gettext.translation('networks', fallback=True)
_ = t.gettext
N_ = t.ngettext
#--------------------------------------------------------------------
# Helper functions
#--------------------------------------------------------------------
@ -56,12 +64,15 @@ class NetworksNoSuchNetworkError(NetworksError):
self._network = network
def __str__(self):
return 'Unknown network %s' % (self._network)
return _('Unknown network %s') % (self._network)
pass
class NetworksNoNetworkSetupError(NetworksError):
"""There are no networks set up."""
def __str__(self):
return _('No networks set up.')
pass

53
mitterlib/network/networkbase.py

@ -24,6 +24,7 @@ The :mod:`networkbase` module defines the base classes for all networks.
"""
import logging
import gettext
from mitterlib.constants import version
@ -39,6 +40,12 @@ def auth_options(namespace, options, auths):
default=None,
help=option['help'],
*(option['flags']))
#--------------------------------------------------------------------
# i18n
#--------------------------------------------------------------------
t = gettext.translation('networks', fallback=True)
_ = t.gettext
N_ = t.ngettext
#--------------------------------------------------------------------
# Exceptions
@ -56,36 +63,77 @@ class NetworkUnknownError(NetworkError):
class NetworkLimitExceededError(NetworkError):
"""The number of requests available was exceeded."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('No more available requests on %s') % (self.name)
pass
class NetworkDNSError(NetworkError):
"""A DNS failure prevented the request to continue."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('DNS failure on a request to %s') % (self.name)
pass
class NetworkInvalidResponseError(NetworkError):
"""The server returned the information in an unexpected way."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('The server on %s return the information in an ' \
'unexpected way.') % (self.name)
pass
class NetworkLowLevelError(NetworkError):
"""A low level error occurred in the network layer."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('Low level error in the network layer when talking ' \
'to %s') % (self.name)
pass
class NetworkBadStatusLineError(NetworkError):
"""Bad status line exception."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('Bad status line in network %s') % (self.name)
pass
class NetworkAuthorizationFailError(NetworkError):
"""Authorization failure."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('Autorization failed for %s') % (self.name)
pass
class NetworkPermissionDeniedError(NetworkError):
"""Permission denied when accessing the message/list."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('Permission denied received on %s') % (self.name)
pass
#--------------------------------------------------------------------
@ -99,6 +147,11 @@ class NetworkWarning(Warning):
class MessageTooLongWarning(NetworkWarning):
"""The message is too long for the network."""
def __init__(self, network_name):
self.name = network_name
def __str__(self):
return _('Message too long for %s') % (self.name)
pass
#--------------------------------------------------------------------

10
mitterlib/network/twitter.py

@ -276,17 +276,17 @@ class Connection(NetworkBase):
# pages.
if exc.code == 403:
# Permission denied.
raise NetworkPermissionDeniedError
raise NetworkInvalidResponseError
raise NetworkPermissionDeniedError(self.NAMESPACE)
raise NetworkInvalidResponseError(self.NAMESPACE)
except urllib2.URLError, exc:
_log.error('URL error: %s' % exc.reason)
raise NetworkDNSError
raise NetworkDNSError(self.NAMESPACE)
except BadStatusLine:
_log.error('Bad status line (Twitter is going bananas)')
raise NetworkBadStatusLineError
raise NetworkBadStatusLineError(self.NAMESPACE)
except socketError: # That's the worst exception ever.
_log.error('Socket connection error')
raise NetworkLowLevelError
raise NetworkLowLevelError(self.NAMESPACE)
# TODO: Permission denied?
# Introduced in Twitter in 2009.03.27

Loading…
Cancel
Save