Skip to content

Commit 61b4409

Browse files
adrienvergedjc
authored andcommitted
Add missing http.Forbidden error (djc#305)
"401 Unauthorized" and "403 Forbidden" are two different HTTP errors. CouchDB uses the first one when the server requires authentication credentials, and the second one when the request requires an authorisation that the current user does not have. For instance, using a `validate_doc_update` design document can result in "403 Forbidden" being returned. See the official documentation [1] for more details and examples: // user is not authorized to make the change but may re-authenticate throw({ unauthorized: 'Error message here.' }); // change is not allowed throw({ forbidden: 'Error message here.' }); This patch adds Forbidden to the list of HTTP errors raised by couchdb-python. [1]: http://docs.couchdb.org/en/stable/couchapp/ddocs.html#validate-document-update-functions
1 parent 370a5b9 commit 61b4409

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

couchdb/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from .client import Database, Document, Server
1010
from .http import HTTPError, PreconditionFailed, Resource, \
11-
ResourceConflict, ResourceNotFound, ServerError, Session, Unauthorized
11+
ResourceConflict, ResourceNotFound, ServerError, Session, \
12+
Unauthorized, Forbidden
1213

1314
try:
1415
__version__ = __import__('pkg_resources').get_distribution('CouchDB').version

couchdb/http.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
from couchdb import util
3939

4040
__all__ = ['HTTPError', 'PreconditionFailed', 'ResourceNotFound',
41-
'ResourceConflict', 'ServerError', 'Unauthorized', 'RedirectLimit',
42-
'Session', 'Resource']
41+
'ResourceConflict', 'ServerError', 'Unauthorized', 'Forbidden',
42+
'RedirectLimit', 'Session', 'Resource']
4343
__docformat__ = 'restructuredtext en'
4444

4545

@@ -114,6 +114,12 @@ class Unauthorized(HTTPError):
114114
"""
115115

116116

117+
class Forbidden(HTTPError):
118+
"""Exception raised when the request requires an authorisation that the
119+
current user does not have.
120+
"""
121+
122+
117123
class RedirectLimit(Exception):
118124
"""Exception raised when a request is redirected more often than allowed
119125
by the maximum number of redirections.
@@ -411,6 +417,8 @@ def _try_request():
411417
error = ''
412418
if status == 401:
413419
raise Unauthorized(error)
420+
elif status == 403:
421+
raise Forbidden(error)
414422
elif status == 404:
415423
raise ResourceNotFound(error)
416424
elif status == 409:

couchdb/tests/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_exports(self):
1111
'Server', 'Database', 'Document',
1212
# couchdb.http
1313
'HTTPError', 'PreconditionFailed', 'ResourceNotFound',
14-
'ResourceConflict', 'ServerError', 'Unauthorized',
14+
'ResourceConflict', 'ServerError', 'Unauthorized', 'Forbidden',
1515
'Resource', 'Session'
1616
])
1717
exported = set(e for e in dir(couchdb) if not e.startswith('_'))

0 commit comments

Comments
 (0)