Merge 0.4.
[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 3 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 urllib
24 from bzrlib.plugins.svn import core
25
26
27 class InvalidExternalsDescription(BzrError):
28     _fmt = """Unable to parse externals description."""
29
30
31 ERR_UNKNOWN_HOSTNAME = 670002
32 ERR_UNKNOWN_HOSTNAME = 670002
33 ERR_RA_SVN_CONNECTION_CLOSED = 210002
34 ERR_WC_LOCKED = 155004
35 ERR_RA_NOT_AUTHORIZED = 170001
36 ERR_INCOMPLETE_DATA = 200003
37 ERR_RA_SVN_MALFORMED_DATA = 210004
38 ERR_RA_NOT_IMPLEMENTED = 170003
39 ERR_FS_NO_SUCH_REVISION = 160006
40 ERR_FS_TXN_OUT_OF_DATE = 160028
41 ERR_REPOS_DISABLED_FEATURE = 165006
42 ERR_STREAM_MALFORMED_DATA = 140001
43 ERR_RA_ILLEGAL_URL = 170000
44 ERR_RA_LOCAL_REPOS_OPEN_FAILED = 180001
45 ERR_BAD_URL = 125002
46 ERR_RA_DAV_REQUEST_FAILED = 175002
47 ERR_RA_DAV_PATH_NOT_FOUND = 175007
48 ERR_FS_NOT_DIRECTORY = 160016
49 ERR_FS_NOT_FOUND = 160013
50 ERR_FS_ALREADY_EXISTS = 160020
51 ERR_RA_SVN_REPOS_NOT_FOUND = 210005
52 ERR_WC_NOT_DIRECTORY = 155007
53 ERR_ENTRY_EXISTS = 150002
54 ERR_WC_PATH_NOT_FOUND = 155010
55 ERR_CANCELLED = 200015
56 ERR_WC_UNSUPPORTED_FORMAT = 155021
57
58
59 class NotSvnBranchPath(NotBranchError):
60     """Error raised when a path was specified that did not exist."""
61     _fmt = """%(path)s is not a valid Subversion branch path. 
62 See 'bzr help svn-branching-schemes' for details."""
63
64     def __init__(self, branch_path, mapping=None):
65         NotBranchError.__init__(self, urllib.quote(branch_path))
66         self.mapping = mapping
67
68
69 class NoSvnRepositoryPresent(NoRepositoryPresent):
70
71     def __init__(self, url):
72         BzrError.__init__(self)
73         self.path = url
74
75
76 class ChangesRootLHSHistory(BzrError):
77     _fmt = """changing lhs branch history not possible on repository root"""
78
79
80 class MissingPrefix(BzrError):
81     _fmt = """Prefix missing for %(path)s; please create it before pushing. """
82
83     def __init__(self, path):
84         BzrError.__init__(self)
85         self.path = path
86
87
88 class RevpropChangeFailed(BzrError):
89     _fmt = """Unable to set revision property %(name)s."""
90
91     def __init__(self, name):
92         BzrError.__init__(self)
93         self.name = name
94
95
96 def convert_error(err):
97     """Convert a Subversion exception to the matching BzrError.
98
99     :param err: SubversionException.
100     :return: BzrError instance if it could be converted, err otherwise
101     """
102     (msg, num) = err.args
103
104     if num == ERR_RA_SVN_CONNECTION_CLOSED:
105         return ConnectionReset(msg=msg)
106     elif num == ERR_WC_LOCKED:
107         return LockError(message=msg)
108     elif num == ERR_RA_NOT_AUTHORIZED:
109         return PermissionDenied('.', msg)
110     elif num == ERR_INCOMPLETE_DATA:
111         return UnexpectedEndOfContainerError()
112     elif num == ERR_RA_SVN_MALFORMED_DATA:
113         return TransportError("Malformed data", msg)
114     elif num == ERR_RA_NOT_IMPLEMENTED:
115         return NotImplementedError("Function not implemented in remote server")
116     elif num == ERR_UNKNOWN_HOSTNAME:
117         return ConnectionError(msg=msg)
118     elif num > 0 and num < 1000:
119         return OSError(num, msg)
120     else:
121         return err
122
123
124 def convert_svn_error(unbound):
125     """Decorator that catches particular Subversion exceptions and 
126     converts them to Bazaar exceptions.
127     """
128     def convert(*args, **kwargs):
129         try:
130             return unbound(*args, **kwargs)
131         except core.SubversionException, e:
132             raise convert_error(e)
133
134     convert.__doc__ = unbound.__doc__
135     convert.__name__ = unbound.__name__
136     return convert
137
138
139 class InvalidPropertyValue(BzrError):
140     _fmt = 'Invalid property value for Subversion property %(property)s: %(msg)s'
141
142     def __init__(self, property, msg):
143         BzrError.__init__(self)
144         self.property = property
145         self.msg = msg
146
147 class RebaseNotPresent(DependencyNotPresent):
148     _fmt = "Unable to import bzr-rebase (required for svn-upgrade support): %(error)s"
149
150     def __init__(self, error):
151         DependencyNotPresent.__init__(self, 'bzr-rebase', error)
152
153
154 class InvalidFileName(BzrError):
155     _fmt = "Unable to convert Subversion path %(path)s because it contains characters invalid in Bazaar."
156
157     def __init__(self, path):
158         BzrError.__init__(self)
159         self.path = path
160
161
162 class CorruptMappingData(BzrError):
163     _fmt = """An invalid change was made to the bzr-specific properties in %(path)s."""
164
165     def __init__(self, path):
166         BzrError.__init__(self)
167         self.path = path
168
169
170 class InvalidSvnBranchPath(NotBranchError):
171     """Error raised when a path was specified that is not a child of or itself
172     a valid branch path in the current branching scheme."""
173     _fmt = """%(path)s is not a valid Subversion branch path in the current 
174 repository layout. See 'bzr help svn-repository-layout' for details."""
175
176     def __init__(self, path, layout):
177         assert isinstance(path, str)
178         NotBranchError.__init__(self, urllib.quote(path))
179         self.layout = layout
180