handle corner case in logwalker.find_latest_change correctly.
[jelmer/subvertpy.git] / errors.py
1 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 """Subversion-specific errors and conversion of Subversion-specific errors."""
17
18 from bzrlib.errors import (BzrError, ConnectionError, ConnectionReset, 
19                            LockError, NotBranchError, PermissionDenied, 
20                            DependencyNotPresent, NoRepositoryPresent,
21                            TransportError, UnexpectedEndOfContainerError)
22
23 import svn.core
24
25 # APR define, not in svn.core
26 SVN_ERR_UNKNOWN_HOSTNAME = 670002
27
28 class NotSvnBranchPath(NotBranchError):
29     """Error raised when a path was specified that did not exist."""
30     _fmt = """%(path)s is not a valid Subversion branch path. 
31 See 'bzr help svn-branching-schemes' for details."""
32
33     def __init__(self, branch_path, scheme=None):
34         NotBranchError.__init__(self, branch_path)
35         self.scheme = scheme
36
37
38 class NoSvnRepositoryPresent(NoRepositoryPresent):
39
40     def __init__(self, url):
41         BzrError.__init__(self)
42         self.path = url
43
44
45 def convert_error(err):
46     """Convert a Subversion exception to the matching BzrError.
47
48     :param err: SubversionException.
49     :return: BzrError instance if it could be converted, err otherwise
50     """
51     (msg, num) = err.args
52
53     if num == svn.core.SVN_ERR_RA_SVN_CONNECTION_CLOSED:
54         return ConnectionReset(msg=msg)
55     elif num == svn.core.SVN_ERR_WC_LOCKED:
56         return LockError(message=msg)
57     elif num == svn.core.SVN_ERR_RA_NOT_AUTHORIZED:
58         return PermissionDenied('.', msg)
59     elif num == svn.core.SVN_ERR_INCOMPLETE_DATA:
60         return UnexpectedEndOfContainerError()
61     elif num == svn.core.SVN_ERR_RA_SVN_MALFORMED_DATA:
62         return TransportError("Malformed data", msg)
63     elif num == SVN_ERR_UNKNOWN_HOSTNAME:
64         return ConnectionError(msg=msg)
65     else:
66         return err
67
68
69 def convert_svn_error(unbound):
70     """Decorator that catches particular Subversion exceptions and 
71     converts them to Bazaar exceptions.
72     """
73     def convert(*args, **kwargs):
74         try:
75             return unbound(*args, **kwargs)
76         except svn.core.SubversionException, e:
77             raise convert_error(e)
78
79     convert.__doc__ = unbound.__doc__
80     convert.__name__ = unbound.__name__
81     return convert
82
83
84 class NoCheckoutSupport(BzrError):
85
86     _fmt = 'Subversion version too old for working tree support.'
87
88
89 class LocalCommitsUnsupported(BzrError):
90
91     _fmt = 'Local commits are not supported for lightweight Subversion checkouts.'
92
93
94 class InvalidPropertyValue(BzrError):
95     _fmt = 'Invalid property value for Subversion property %(property)s: %(msg)s'
96
97     def __init__(self, property, msg):
98         BzrError.__init__(self)
99         self.property = property
100         self.msg = msg
101
102 class RebaseNotPresent(DependencyNotPresent):
103     _fmt = "Unable to import bzr-rebase (required for svn-upgrade support): %(error)s"
104
105     def __init__(self, error):
106         DependencyNotPresent.__init__(self, 'bzr-rebase', error)