marco@16: #!/usr/bin/env python
marco@16: # -*- coding: utf-8 -*-
marco@16: """
marco@16: Provides a convenience class for handling and parsing Error Document responses.
marco@16: """
marco@16:
marco@16: from deposit_receipt import Deposit_Receipt
marco@16: from server_errors import SWORD2ERRORSBYIRI, get_error
marco@16:
marco@16: class Error_Document(Deposit_Receipt):
marco@16: """
marco@16: Example Error document:
marco@16:
marco@16:
marco@16:
marco@16:
marco@16: Example repository
marco@16:
marco@16: ERROR
marco@16: 2008-02-19T09:34:27Z
marco@16:
marco@16: sword@example.org
marco@16:
marco@16: The manifest could be parsed, but was not valid -
marco@16: no technical metadata was provided.
marco@16: processing failed
marco@16:
marco@16: Exception at [ ... ]
marco@16:
marco@16:
marco@16:
marco@16:
marco@16:
marco@16:
marco@16: Error document is an AtomPub extension:
marco@16:
marco@16: The sword:error element MAY contain any of the elements normally used in the Deposit Receipt, but all fields are OPTIONAL.
marco@16:
marco@16: The error document SHOULD contain an atom:summary element with a short description of the error.
marco@16:
marco@16: The error document MAY contain a sword:verboseDescription element with a long description of the problem or any other appropriate software-level debugging output (e.g. a stack trace). Server implementations may wish to provide this for client developers' convenience, but may wish to disable such output in any production systems.
marco@16:
marco@16: The server SHOULD specify that the Content-Type of the is text/xml or application/xml.
marco@16: """
marco@16: def __init__(self, xml_deposit_receipt=None, code=None, resp=None):
marco@16: if xml_deposit_receipt:
marco@16: super(Error_Document, self).__init__(xml_deposit_receipt)
marco@16: else:
marco@16: super(Error_Document, self).__init__("""
marco@16: """)
marco@16: self.error_href = None
marco@16: self.error_info = None
marco@16: self.verboseDescription = None
marco@16: self.content = None # for parity with the ContentWrapper
marco@16: self.code = code
marco@16: self.response_headers = resp
marco@16: self._characterise_error()
marco@16:
marco@16: def _characterise_error(self):
marco@16: if "sword_verboseDescription" in self.metadata.keys():
marco@16: self.verboseDescription = self.metadata['sword_verboseDescription']
marco@16: if "href" in self.dom.attrib.keys():
marco@16: self.error_href = self.dom.attrib['href']
marco@16: self.error_info = get_error(self.error_href, self.code)