Changeset 1481:6e82349a3a14


Ignore:
Timestamp:
Apr 19, 2014, 6:30:52 PM (6 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use original exception message in `FTPError` (ticket #76).

If we create an `FTPError` instance based on a previously raised
`ftplib` or `socket` exception, take the string representation of this
original exception as the string representation of the new `FTPError`
exception.

The `*args` passed to `FTPError` aren't necessarily enough to
guarantee this. At least for some socket exceptions, `args` is a tuple
of an integer and a string and using only `args[0]` to form the
exception message keeps only the integer.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/error.py

    r1455 r1481  
    1 # Copyright (C) 2003-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2003-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# See the file LICENSE for licensing terms.
    33
     
    3131  "SyncError",
    3232  "FTPIOError",
    33   ]
     33]
    3434
    3535
     
    3737    """General ftputil error class."""
    3838
    39     def __init__(self, *args):
     39    # In Python 2, we can't use a keyword argument after `*args`, so
     40    # `pop` from `**kwargs`.
     41    def __init__(self, *args, **kwargs):
    4042        super(FTPError, self).__init__(*args)
    41         # Don't use `args[0]` directly because `args` may be empty.
    42         if args:
    43             self.strerror = self.args[0]
     43        if "original_exception" in kwargs:
     44            self.strerror = ftputil.compat.unicode_type(
     45                              kwargs.pop("original_exception"))
     46        elif args:
     47            # If there was no `original_exception` argument, assume
     48            # the first argument is a unicode string.
     49            self.strerror = args[0]
    4450        else:
    4551            self.strerror = ""
     
    131137                raise PermanentError(*exc_value.args)
    132138        elif isinstance(exc_value, ftplib.all_errors):
    133             raise FTPOSError(*exc_value.args)
     139            raise FTPOSError(*exc_value.args, original_exception=exc_value)
    134140        else:
    135141            raise
  • test/test_error.py

    r1480 r1481  
    11# encoding: utf-8
    2 # Copyright (C) 2002-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     2# Copyright (C) 2002-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    33# See the file LICENSE for licensing terms.
    44
     
    6363            self.fail()
    6464
     65    def test_error_message_reuse(self):
     66        """
     67        Test if the error message string is retained if the caugt
     68        exception has more than one element in `args`.
     69        """
     70        # See ticket #76.
     71        try:
     72            # Format "host:port" doesn't work.
     73            host = ftputil.FTPHost("localhost:21", "", "")
     74        except ftputil.error.FTPOSError as exc:
     75            # The error message might change for future Python
     76            # versions, so possibly relax the assertion later.
     77            self.assertTrue("[Errno -2] Name or service not known" in
     78                            str(exc))
     79
    6580
    6681if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.