~ubuntu-branches/debian/experimental/lazr.restfulclient/experimental

« back to all changes in this revision

Viewing changes to src/lazr/restfulclient/errors.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2010-07-24 15:54:08 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100724155408-utbdh42cijnipvva
Tags: 0.9.21-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
__metaclass__ = type
22
22
__all__ = [
 
23
    'BadRequest',
 
24
    'Conflict',
 
25
    'ClientError',
23
26
    'CredentialsError',
24
27
    'CredentialsFileError',
25
28
    'HTTPError',
 
29
    'MethodNotAllowed',
 
30
    'NotFound',
 
31
    'PreconditionFailed',
26
32
    'RestfulError',
27
33
    'ResponseError',
 
34
    'ServerError',
 
35
    'Unauthorized',
28
36
    'UnexpectedResponseError',
29
37
    ]
30
38
 
68
76
                "Response headers:\n---\n%s\n---\n"
69
77
                "Response body:\n---\n%s\n---\n") % (
70
78
            self.response.status, self.response.reason, headers, self.content)
 
79
 
 
80
 
 
81
class ClientError(HTTPError):
 
82
    """An exception representing a client-side error."""
 
83
 
 
84
 
 
85
class Unauthorized(ClientError):
 
86
    """An exception representing an authentication failure."""
 
87
 
 
88
 
 
89
class NotFound(ClientError):
 
90
    """An exception representing a nonexistent resource."""
 
91
 
 
92
 
 
93
class MethodNotAllowed(ClientError):
 
94
    """An exception raised when you use an unsupported HTTP method.
 
95
 
 
96
    This is most likely because you tried to delete a resource that
 
97
    can't be deleted.
 
98
    """
 
99
 
 
100
 
 
101
class BadRequest(ClientError):
 
102
    """An exception representing a problem with a client request."""
 
103
 
 
104
 
 
105
class Conflict(ClientError):
 
106
    """An exception representing a conflict with another client."""
 
107
 
 
108
 
 
109
class PreconditionFailed(ClientError):
 
110
    """An exception representing the failure of a conditional PUT/PATCH.
 
111
 
 
112
    The most likely explanation is that another client changed this
 
113
    object while you were working on it, and your version of the
 
114
    object is now out of date.
 
115
    """
 
116
 
 
117
class ServerError(HTTPError):
 
118
    """An exception representing a server-side error."""
 
119
 
 
120
 
 
121
def error_for(response, content):
 
122
    """Turn an HTTP response into an HTTPError subclass.
 
123
 
 
124
    :return: None if the response code is 1xx, 2xx or 3xx. Otherwise,
 
125
    an instance of an appropriate HTTPError subclass (or HTTPError
 
126
    if nothing else is appropriate.
 
127
    """
 
128
    http_errors_by_status_code = {
 
129
        400 : BadRequest,
 
130
        401 : Unauthorized,
 
131
        404 : NotFound,
 
132
        405 : MethodNotAllowed,
 
133
        409 : Conflict,
 
134
        412 : PreconditionFailed,
 
135
    }
 
136
 
 
137
    if response.status // 100 <= 3:
 
138
        # 1xx, 2xx and 3xx are not considered errors.
 
139
        return None
 
140
    else:
 
141
        cls = http_errors_by_status_code.get(response.status, HTTPError)
 
142
    if cls is HTTPError:
 
143
        if response.status // 100 == 5:
 
144
            cls = ServerError
 
145
        elif response.status // 100 == 4:
 
146
            cls = ClientError
 
147
    return cls(response, content)