source: ftp_error.py @ 618:d72cf4f7bb29

Last change on this file since 618:d72cf4f7bb29 was 618:d72cf4f7bb29, checked in by Stefan Schwarzer <sschwarzer@…>, 14 years ago
Let `FTPError` inherit from `Exception`.
File size: 3.6 KB
Line 
1# Copyright (C) 2003-2006, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# - Redistributions of source code must retain the above copyright
9#   notice, this list of conditions and the following disclaimer.
10#
11# - Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14#
15# - Neither the name of the above author nor the names of the
16#   contributors to the software may be used to endorse or promote
17#   products derived from this software without specific prior written
18#   permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
24# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32"""
33ftp_error.py - exception classes and wrappers
34"""
35
36# $Id$
37
38import ftplib
39import sys
40
41import ftputil_version
42
43
44class FTPError(Exception):
45    """General error class."""
46    def __init__(self, ftp_exception):
47        self.args = (ftp_exception,)
48        self.strerror = str(ftp_exception)
49        try:
50            self.errno = int(self.strerror[:3])
51        except (TypeError, IndexError, ValueError):
52            self.errno = None
53        self.filename = None
54
55    def __str__(self):
56        return "%s\nDebugging info: %s" % \
57               (self.strerror, ftputil_version.version_info)
58
59# internal errors are those that have more to do with the inner
60#  workings of ftputil than with errors on the server side
61class InternalError(FTPError): pass
62class RootDirError(InternalError): pass
63class InaccessibleLoginDirError(InternalError): pass
64class TimeShiftError(InternalError): pass
65class ParserError(InternalError): pass
66class KeepAliveError(InternalError): pass
67
68class FTPOSError(FTPError, OSError): pass
69class TemporaryError(FTPOSError): pass
70class PermanentError(FTPOSError): pass
71
72#XXX Do you know better names for `_try_with_oserror` and
73#    `_try_with_ioerror`?
74def _try_with_oserror(callee, *args, **kwargs):
75    """
76    Try the callee with the given arguments and map resulting
77    exceptions from `ftplib.all_errors` to `FTPOSError` and its
78    derived classes.
79    """
80    try:
81        return callee(*args, **kwargs)
82    except ftplib.error_temp, obj:
83        raise TemporaryError(obj)
84    except ftplib.error_perm, obj:
85        raise PermanentError(obj)
86    except ftplib.all_errors:
87        ftp_error = sys.exc_info()[1]
88        raise FTPOSError(ftp_error)
89
90class FTPIOError(FTPError, IOError): pass
91
92def _try_with_ioerror(callee, *args, **kwargs):
93    """
94    Try the callee with the given arguments and map resulting
95    exceptions from `ftplib.all_errors` to `FTPIOError`.
96    """
97    try:
98        return callee(*args, **kwargs)
99    except ftplib.all_errors:
100        ftp_error = sys.exc_info()[1]
101        raise FTPIOError(ftp_error)
102
Note: See TracBrowser for help on using the repository browser.