marco@16
|
1 #!/usr/bin/env python
|
marco@16
|
2 # -*- coding: utf-8 -*-
|
marco@16
|
3 """
|
marco@16
|
4 Provides a convenience class for handling and parsing Error Document responses.
|
marco@16
|
5 """
|
marco@16
|
6
|
marco@16
|
7 from deposit_receipt import Deposit_Receipt
|
marco@16
|
8 from server_errors import SWORD2ERRORSBYIRI, get_error
|
marco@16
|
9
|
marco@16
|
10 class Error_Document(Deposit_Receipt):
|
marco@16
|
11 """
|
marco@16
|
12 Example Error document:
|
marco@16
|
13
|
marco@16
|
14 <?xml version="1.0" encoding="utf-8"?>
|
marco@16
|
15 <sword:error xmlns="http://www.w3.org/2005/Atom"
|
marco@16
|
16 xmlns:sword="http://purl.org/net/sword/"
|
marco@16
|
17 xmlns:arxiv="http://arxiv.org/schemas/atom"
|
marco@16
|
18 href="http://example.org/errors/BadManifest">
|
marco@16
|
19 <author>
|
marco@16
|
20 <name>Example repository</name>
|
marco@16
|
21 </author>
|
marco@16
|
22 <title>ERROR</title>
|
marco@16
|
23 <updated>2008-02-19T09:34:27Z</updated>
|
marco@16
|
24
|
marco@16
|
25 <generator uri="https://example.org/sword-app/"
|
marco@16
|
26 version="0.9">sword@example.org</generator>
|
marco@16
|
27
|
marco@16
|
28 <summary>The manifest could be parsed, but was not valid -
|
marco@16
|
29 no technical metadata was provided.</summary>
|
marco@16
|
30 <sword:treatment>processing failed</sword:treatment>
|
marco@16
|
31 <sword:verboseDescription>
|
marco@16
|
32 Exception at [ ... ]
|
marco@16
|
33 </sword:verboseDescription>
|
marco@16
|
34 <link rel="alternate" href="https://arxiv.org/help" type="text/html"/>
|
marco@16
|
35
|
marco@16
|
36 </sword:error>
|
marco@16
|
37
|
marco@16
|
38
|
marco@16
|
39 Error document is an AtomPub extension:
|
marco@16
|
40
|
marco@16
|
41 The sword:error element MAY contain any of the elements normally used in the Deposit Receipt, but all fields are OPTIONAL.
|
marco@16
|
42
|
marco@16
|
43 The error document SHOULD contain an atom:summary element with a short description of the error.
|
marco@16
|
44
|
marco@16
|
45 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
|
46
|
marco@16
|
47 The server SHOULD specify that the Content-Type of the is text/xml or application/xml.
|
marco@16
|
48 """
|
marco@16
|
49 def __init__(self, xml_deposit_receipt=None, code=None, resp=None):
|
marco@16
|
50 if xml_deposit_receipt:
|
marco@16
|
51 super(Error_Document, self).__init__(xml_deposit_receipt)
|
marco@16
|
52 else:
|
marco@16
|
53 super(Error_Document, self).__init__("""<?xml version="1.0" encoding="utf-8"?>
|
marco@16
|
54 <sword:error xmlns:sword="http://purl.org/net/sword/"/>""")
|
marco@16
|
55 self.error_href = None
|
marco@16
|
56 self.error_info = None
|
marco@16
|
57 self.verboseDescription = None
|
marco@16
|
58 self.content = None # for parity with the ContentWrapper
|
marco@16
|
59 self.code = code
|
marco@16
|
60 self.response_headers = resp
|
marco@16
|
61 self._characterise_error()
|
marco@16
|
62
|
marco@16
|
63 def _characterise_error(self):
|
marco@16
|
64 if "sword_verboseDescription" in self.metadata.keys():
|
marco@16
|
65 self.verboseDescription = self.metadata['sword_verboseDescription']
|
marco@16
|
66 if "href" in self.dom.attrib.keys():
|
marco@16
|
67 self.error_href = self.dom.attrib['href']
|
marco@16
|
68 self.error_info = get_error(self.error_href, self.code)
|