Merge pull request #627 from Unrud/improveerrors

Small improvements for error handling
This commit is contained in:
Unrud 2017-06-07 14:37:30 +02:00 committed by GitHub
commit 82fc6f7eb6
3 changed files with 20 additions and 11 deletions

View File

@ -842,6 +842,4 @@ class Application:
headers = {"Content-Type": "text/xml; charset=%s" % self.encoding} headers = {"Content-Type": "text/xml; charset=%s" % self.encoding}
status, xml_answer = xmlutils.report( status, xml_answer = xmlutils.report(
base_prefix, path, xml_content, collection) base_prefix, path, xml_content, collection)
if status == client.PRECONDITION_FAILED:
return PRECONDITION_FAILED
return (status, headers, self._write_xml_content(xml_answer)) return (status, headers, self._write_xml_content(xml_answer))

View File

@ -1268,17 +1268,20 @@ class Collection(BaseCollection):
flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0 flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
overlapped = Overlapped() overlapped = Overlapped()
if not lock_file_ex(handle, flags, 0, 1, 0, overlapped): if not lock_file_ex(handle, flags, 0, 1, 0, overlapped):
raise RuntimeError("Locking the storage failed: %s" % raise RuntimeError("Locking the storage failed "
ctypes.FormatError()) "(can be disabled in the config): "
"%s" % ctypes.FormatError())
elif os.name == "posix": elif os.name == "posix":
_cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH _cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH
try: try:
fcntl.flock(cls._lock_file.fileno(), _cmd) fcntl.flock(cls._lock_file.fileno(), _cmd)
except OSError as e: except OSError as e:
raise RuntimeError("Locking the storage failed: %s" % raise RuntimeError("Locking the storage failed "
e) from e "(can be disabled in the config): "
"%s" % e) from e
else: else:
raise RuntimeError("Locking the storage failed: " raise RuntimeError("Locking the storage failed "
"(can be disabled in the config): "
"Unsupported operating system") "Unsupported operating system")
cls._lock_file_locked = True cls._lock_file_locked = True
try: try:

View File

@ -127,6 +127,13 @@ def _href(base_prefix, href):
return quote("%s%s" % (base_prefix, href)) return quote("%s%s" % (base_prefix, href))
def _webdav_error(namespace, name):
"""Generate XML error message."""
root = ET.Element(_tag("D", "error"))
root.append(ET.Element(_tag(namespace, name)))
return root
def _date_to_datetime(date_): def _date_to_datetime(date_):
"""Transform a date to a UTC datetime. """Transform a date to a UTC datetime.
@ -997,7 +1004,7 @@ def report(base_prefix, path, xml_request, collection):
""" """
multistatus = ET.Element(_tag("D", "multistatus")) multistatus = ET.Element(_tag("D", "multistatus"))
if xml_request is None: if xml_request is None:
return multistatus return client.MULTI_STATUS, multistatus
root = xml_request root = xml_request
if root.tag in ( if root.tag in (
_tag("D", "principal-search-property-set"), _tag("D", "principal-search-property-set"),
@ -1039,9 +1046,10 @@ def report(base_prefix, path, xml_request, collection):
sync_token, names = collection.sync(old_sync_token) sync_token, names = collection.sync(old_sync_token)
except ValueError as e: except ValueError as e:
# Invalid sync token # Invalid sync token
collection.logger.info("Client provided invalid sync token %r: %s", collection.logger.warning("Client provided invalid sync token %r: "
old_sync_token, e, exc_info=True) "%s", old_sync_token, e, exc_info=True)
return client.PRECONDITION_FAILED, None return (client.PRECONDITION_FAILED,
_webdav_error("D", "valid-sync-token"))
hreferences = ("/" + posixpath.join(collection.path, n) for n in names) hreferences = ("/" + posixpath.join(collection.path, n) for n in names)
# Append current sync token to response # Append current sync token to response
sync_token_element = ET.Element(_tag("D", "sync-token")) sync_token_element = ET.Element(_tag("D", "sync-token"))