diff --git a/src/caosdb/common/administration.py b/src/caosdb/common/administration.py index f88119eb7c89dc5189d14a1d4c654b5cde0061bf..7f9336c7473ac759aab4461c84b708a3b9049d8b 100644 --- a/src/caosdb/common/administration.py +++ b/src/caosdb/common/administration.py @@ -31,8 +31,8 @@ from lxml import etree from caosdb.common.utils import xml2str from caosdb.connection.connection import get_connection from caosdb.exceptions import (HTTPClientError, - HTTPAuthorizationError, - ResourceNotFoundError) + HTTPForbiddenError, + HTTPResourceNotFoundError) def set_server_property(key, value): @@ -107,10 +107,10 @@ def _retrieve_user(name, realm=None, **kwargs): con = get_connection() try: return con._http_request(method="GET", path="User/" + (realm + "/" + name if realm is not None else name), **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to retrieve this user." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "User does not exist." raise @@ -119,10 +119,10 @@ def _delete_user(name, **kwargs): con = get_connection() try: return con._http_request(method="DELETE", path="User/" + name, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to delete this user." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "User does not exist." raise @@ -145,10 +145,10 @@ def _update_user(name, realm=None, password=None, status=None, params["entity"] = str(entity) try: return con.put_form_data(entity_uri_segment="User/" + (realm + "/" + name if realm is not None else name), params=params, **kwargs).read() - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "User does not exist." raise - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to update this user." raise except HTTPClientError as e: @@ -174,7 +174,7 @@ def _insert_user(name, password=None, status=None, email=None, entity=None, **kw params["entity"] = entity try: return con.post_form_data(entity_uri_segment="User", params=params, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to insert a new user." raise e except HTTPClientError as e: @@ -190,7 +190,7 @@ def _insert_role(name, description, **kwargs): con = get_connection() try: return con.post_form_data(entity_uri_segment="Role", params={"role_name": name, "role_description": description}, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to insert a new role." raise except HTTPClientError as e: @@ -203,10 +203,10 @@ def _update_role(name, description, **kwargs): con = get_connection() try: return con.put_form_data(entity_uri_segment="Role/" + name, params={"role_description": description}, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to update this role." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "Role does not exist." raise @@ -215,10 +215,10 @@ def _retrieve_role(name, **kwargs): con = get_connection() try: return con._http_request(method="GET", path="Role/" + name, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to retrieve this role." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "Role does not exist." raise @@ -227,10 +227,10 @@ def _delete_role(name, **kwargs): con = get_connection() try: return con._http_request(method="DELETE", path="Role/" + name, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to delete this role." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "Role does not exist." raise @@ -246,10 +246,10 @@ def _set_roles(username, roles, realm=None, **kwargs): try: body = con._http_request(method="PUT", path="UserRoles/" + (realm + "/" + username if realm is not None else username), body=body, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to set this user's roles." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "User does not exist." raise except HTTPClientError as e: @@ -270,10 +270,10 @@ def _get_roles(username, realm=None, **kwargs): try: body = con._http_request(method="GET", path="UserRoles/" + ( realm + "/" + username if realm is not None else username), **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to retrieve this user's roles." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "User does not exist." raise ret = set() @@ -313,10 +313,10 @@ Returns con = get_connection() try: return con._http_request(method="PUT", path="PermissionRules/" + role, body=body, **kwargs).read() - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to set this role's permissions." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "Role does not exist." raise @@ -325,10 +325,10 @@ def _get_permissions(role, **kwargs): con = get_connection() try: return PermissionRule._parse_body(con._http_request(method="GET", path="PermissionRules/" + role, **kwargs).read()) - except HTTPAuthorizationError as e: + except HTTPForbiddenError as e: e.msg = "You are not permitted to retrieve this role's permissions." raise - except ResourceNotFoundError as e: + except HTTPResourceNotFoundError as e: e.msg = "Role does not exist." raise diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 6450587afb7043bcda298eb6eccd85e54c6e61ba..cd0683ed5e4ec7a6e61f7474ec9e58e3878e24bc 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -58,7 +58,7 @@ from caosdb.exceptions import (AmbiguousEntityError, UniqueNamesError, UnqualifiedParentsError, UnqualifiedPropertiesError, - URITooLongError) + HTTPURITooLongError) from lxml import etree _ENTITY_URI_SEGMENT = "Entity" @@ -2939,7 +2939,7 @@ class Container(list): "&".join(entities))], query_dict=flags) return Container._response_to_entities(http_response) - except URITooLongError as uri_e: + except HTTPURITooLongError as uri_e: try: # split up uri1, uri2 = Container._split_uri_string(entities) diff --git a/src/caosdb/connection/connection.py b/src/caosdb/connection/connection.py index d7e4e2cd2b6bf2edd832c8a956d25e61587ac1b6..fc699ab5db1db36bc1ee63034b6828eec4d16bc1 100644 --- a/src/caosdb/connection/connection.py +++ b/src/caosdb/connection/connection.py @@ -36,11 +36,11 @@ from caosdb.configuration import get_config from caosdb.exceptions import (CaosDBException, HTTPClientError, ConfigurationError, CaosDBConnectionError, - HTTPAuthorizationError, + HTTPForbiddenError, LoginFailedError, - ResourceNotFoundError, + HTTPResourceNotFoundError, HTTPServerError, - URITooLongError) + HTTPURITooLongError) from caosdb.version import version from pkg_resources import resource_filename @@ -64,6 +64,10 @@ class _WrappedHTTPResponse(CaosDBHTTPResponse): def __init__(self, response): self.response = response + @property + def reason(self): + return self.response.reason + @property def status(self): return self.response.status @@ -402,29 +406,24 @@ def _handle_response_status(http_response): # emtpy response buffer body = http_response.read() + if status == 404: + raise HTTPResourceNotFoundError("This resource has not been found.") + elif status > 499: + raise HTTPServerError(body=body) + + reason = http_response.reason + standard_message = ("Request failed. The response returned with status " + "{} - {}.".format(status, reason)) if status == 401: - raise LoginFailedError( - "Request failed. The response returned with status " - "{}.".format(status)) + raise LoginFailedError(standard_message) elif status == 403: - raise HTTPAuthorizationError( - "Request failed. The response returned with status " - "{}.".format(status)) - elif status == 404: - raise ResourceNotFoundError("This entity does not exist.") + raise HTTPForbiddenError(standard_message) elif status in (413, 414): - raise URITooLongError( - "Request failed. The response returned with status " - "{}.".format(status)) + raise HTTPURITooLongError(standard_message) elif 399 < status < 500: - raise HTTPClientError(msg=("Request failed. The response returned " - "with status {}.").format(status), status=status, body=body) - elif status > 499: - raise HTTPServerError(body=body) + raise HTTPClientError(msg=standard_message, status=status, body=body) else: - raise CaosDBException( - "Request failed. The response returned with status " - "{}.".format(status)) + raise CaosDBException(standard_message) class _Connection(object): # pylint: disable=useless-object-inheritance @@ -558,8 +557,8 @@ class _Connection(object): # pylint: disable=useless-object-inheritance uri_segments.extend(path.split("/")) return self.retrieve(entity_uri_segments=uri_segments) - except ResourceNotFoundError: - raise ResourceNotFoundError("This file does not exist.") + except HTTPResourceNotFoundError: + raise HTTPResourceNotFoundError("This file does not exist.") def _login(self): self._authenticator.login() diff --git a/src/caosdb/exceptions.py b/src/caosdb/exceptions.py index 8c462926ffb44c96a57e5eefdfa7291a51b1bf53..c9da22ce59b4eee37ea35e58d24ef436a6e88255 100644 --- a/src/caosdb/exceptions.py +++ b/src/caosdb/exceptions.py @@ -92,7 +92,7 @@ class CaosDBConnectionError(CaosDBException): CaosDBException.__init__(self, msg) -class URITooLongError(HTTPClientError): +class HTTPURITooLongError(HTTPClientError): """The URI of the last request was too long.""" def __init__(self, msg=None): @@ -109,7 +109,7 @@ class LoginFailedError(CaosDBException): CaosDBException.__init__(self, msg=msg) -class HTTPAuthorizationError(HTTPClientError): +class HTTPForbiddenError(HTTPClientError): """You're lacking the required permissions. Corresponds to HTTP status 403. @@ -119,7 +119,7 @@ class HTTPAuthorizationError(HTTPClientError): HTTPClientError.__init__(self, msg=msg, status=403, body=None) -class ResourceNotFoundError(HTTPClientError): +class HTTPResourceNotFoundError(HTTPClientError): """The requested resource doesn't exist; corresponds to HTTP status 404.