root/xcap/errors.py

Revision 417, 3.8 kB (checked in by Denis Bilenko <denis@ag-projects.com>, 5 weeks ago)

added <entry field=...> to <uniqueness-failure> (required by schema)

Line 
1# Copyright (C) 2007 AG Projects.
2#
3
4"""XCAP errors module"""
5
6from xml.sax.saxutils import quoteattr
7from twisted.web2 import http_headers
8from twisted.web2.http import Response, HTTPError
9
10__all__ = [
11    'XCAPError',
12    'ResourceNotFound',
13    'NotWellFormedError', 'SchemaValidationError', 'NotUTF8Error', 'NotXMLAtrributeValueError',
14    'NotXMLFragmentError', 'CannotInsertError', 'CannotDeleteError', 'NoParentError',
15    'UniquenessFailureError', 'ConstraintFailureError']
16
17class ResourceNotFound(HTTPError):
18    def __init__(self, msg=""):
19        self.msg = msg
20        HTTPError.__init__(self, Response(404, stream=msg))
21
22    def __str__(self):
23        return self.msg
24
25
26class XCAPError(HTTPError):
27
28    code = 409
29    namespace = "urn:ietf:params:xml:ns:xcap-error"
30    tag = "undefined"
31    phrase = ''
32
33    def __init__(self, phrase=None, comment=''):
34        if phrase is not None:
35            self.phrase = phrase
36        if comment:
37            self.comment = '<!--\n' + str(comment).replace('-->', '--&gt;') + '\n-->'
38        else:
39            self.comment = ''
40        self.response = ErrorResponse(self.code, self.build_xml_output())
41        HTTPError.__init__(self, self.response)
42
43    def build_xml_output(self):
44        return """<?xml version="1.0" encoding="UTF-8"?>
45<xcap-error xmlns="%s">%s</xcap-error>""" % (self.namespace, self.format_my_tag())
46
47    def format_my_body(self):
48        return ''
49
50    def format_my_phrase(self):
51        if self.phrase:
52            return ' phrase=%s' % quoteattr(self.phrase)
53        else:
54            return ''
55
56    def format_my_tag(self):
57        phrase_attr = self.format_my_phrase()
58        body = self.format_my_body()
59        if body or self.comment:
60            return '<%s%s>%s%s</%s>' % (self.tag, phrase_attr, self.comment, body, self.tag)
61        else:
62            return '<%s%s/>' % (self.tag, phrase_attr)
63
64    def __str__(self):
65        try:
66            return self.format_my_tag()
67        except:
68            return ''
69
70
71class ErrorResponse(Response):
72    """
73    A L{Response} object which simply contains a status code and a description of
74    what happened.
75    """
76
77    def __init__(self, code, output):
78        """
79        @param code: a response code in L{responsecode.RESPONSES}.
80        @param output: the body to be attached to the response
81        """
82
83        output = output.encode("utf-8")
84        mime_params = {"charset": "utf-8"}
85
86        Response.__init__(self, code=code, stream=output)
87
88        ## Its MIME type, registered by this specification, is "application/xcap-error+xml".
89        self.headers.setHeader("content-type", http_headers.MimeType("application", "xcap-error+xml", mime_params))
90
91class SchemaValidationError(XCAPError):
92    tag = "schema-validation-error"
93
94class NotXMLFragmentError(XCAPError):
95    tag = "not-xml-frag"
96
97class CannotInsertError(XCAPError):
98    tag = "cannot-insert"
99
100class CannotDeleteError(XCAPError):
101    tag = "cannot-delete"
102
103class NotXMLAtrributeValueError(XCAPError):
104    tag = "not-xml-att-value"
105
106class NotWellFormedError(XCAPError):
107    tag = "not-well-formed"
108
109class ConstraintFailureError(XCAPError):
110    tag = "constraint-failure"
111
112class NotUTF8Error(XCAPError):
113    tag = "not-utf-8"
114
115class NoParentError(XCAPError):
116    tag = "no-parent"
117
118    def __init__(self, phrase='', ancestor='', comment=''):
119        self.ancestor = ancestor
120        XCAPError.__init__(self, phrase, comment)
121
122    def format_my_body(self):
123        if self.ancestor:
124            return "<ancestor>%s</ancestor>" % self.ancestor
125        else:
126            return ""
127
128class UniquenessFailureError(XCAPError):
129    tag = "uniqueness-failure"
130
131    def __init__(self, **kwargs):
132        self.exists = kwargs.pop('exists')
133        XCAPError.__init__(self, **kwargs)
134
135    def format_my_body(self):
136        return "<exists field=%s/>" % quoteattr(self.exists)
Note: See TracBrowser for help on using the browser.