Package restkit :: Module errors
[hide private]

Source Code for Module restkit.errors

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license. 
  4  # See the NOTICE for more information. 
  5   
  6  """ 
  7  exception classes. 
  8  """ 
  9   
10 -class ResourceError(Exception):
11 """ default error class """ 12 13 status_int = None 14
15 - def __init__(self, msg=None, http_code=None, response=None):
16 self.msg = msg or '' 17 self.status_int = http_code or self.status_int 18 self.response = response 19 Exception.__init__(self)
20
21 - def _get_message(self):
22 return self.msg
23 - def _set_message(self, msg):
24 self.msg = msg or ''
25 message = property(_get_message, _set_message) 26
27 - def __str__(self):
28 if self.msg: 29 return self.msg 30 try: 31 return str(self.__dict__) 32 except (NameError, ValueError, KeyError), e: 33 return 'Unprintable exception %s: %s' \ 34 % (self.__class__.__name__, str(e))
35 36
37 -class ResourceNotFound(ResourceError):
38 """Exception raised when no resource was found at the given url. 39 """ 40 status_int = 404
41
42 -class Unauthorized(ResourceError):
43 """Exception raised when an authorization is required to access to 44 the resource specified. 45 """
46
47 -class ResourceGone(ResourceError):
48 """ 49 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11 50 """ 51 status_int = 410
52
53 -class RequestFailed(ResourceError):
54 """Exception raised when an unexpected HTTP error is received in response 55 to a request. 56 57 58 The request failed, meaning the remote HTTP server returned a code 59 other than success, unauthorized, or NotFound. 60 61 The exception message attempts to extract the error 62 63 You can get the status code by e.status_int, or see anything about the 64 response via e.response. For example, the entire result body (which is 65 probably an HTML error page) is e.response.body. 66 """
67
68 -class RedirectLimit(Exception):
69 """Exception raised when the redirection limit is reached."""
70
71 -class RequestError(Exception):
72 """Exception raised when a request is malformed"""
73
74 -class RequestTimeout(Exception):
75 """ Exception raised on socket timeout """
76
77 -class InvalidUrl(Exception):
78 """ 79 Not a valid url for use with this software. 80 """
81
82 -class ResponseError(Exception):
83 """ Error raised while getting response or decompressing response stream"""
84 85
86 -class ProxyError(Exception):
87 """ raised when proxy error happend"""
88
89 -class BadStatusLine(Exception):
90 """ Exception returned by the parser when the status line is invalid""" 91 pass
92
93 -class ParserError(Exception):
94 """ Generic exception returned by the parser """ 95 pass
96
97 -class UnexpectedEOF(Exception):
98 """ exception raised when remote closed the connection """
99
100 -class AlreadyRead(Exception):
101 """ raised when a response have already been read """
102
103 -class ProxyError(Exception):
104 pass
105 106 ############################# 107 # HTTP parser errors 108 ############################# 109
110 -class ParseException(Exception):
111 pass
112
113 -class NoMoreData(ParseException):
114 - def __init__(self, buf=None):
115 self.buf = buf
116 - def __str__(self):
117 return "No more data after: %r" % self.buf
118
119 -class InvalidRequestLine(ParseException):
120 - def __init__(self, req):
121 self.req = req 122 self.code = 400
123
124 - def __str__(self):
125 return "Invalid HTTP request line: %r" % self.req
126
127 -class InvalidRequestMethod(ParseException):
128 - def __init__(self, method):
129 self.method = method
130
131 - def __str__(self):
132 return "Invalid HTTP method: %r" % self.method
133
134 -class InvalidHTTPVersion(ParseException):
135 - def __init__(self, version):
136 self.version = version
137
138 - def __str__(self):
139 return "Invalid HTTP Version: %s" % self.version
140
141 -class InvalidHTTPStatus(ParseException):
142 - def __init__(self, status):
143 self.status = status
144
145 - def __str__(self):
146 return "Invalid HTTP Status: %s" % self.status
147
148 -class InvalidHeader(ParseException):
149 - def __init__(self, hdr):
150 self.hdr = hdr
151
152 - def __str__(self):
153 return "Invalid HTTP Header: %r" % self.hdr
154
155 -class InvalidHeaderName(ParseException):
156 - def __init__(self, hdr):
157 self.hdr = hdr
158
159 - def __str__(self):
160 return "Invalid HTTP header name: %r" % self.hdr
161
162 -class InvalidChunkSize(ParseException):
163 - def __init__(self, data):
164 self.data = data
165
166 - def __str__(self):
167 return "Invalid chunk size: %r" % self.data
168
169 -class ChunkMissingTerminator(ParseException):
170 - def __init__(self, term):
171 self.term = term
172
173 - def __str__(self):
174 return "Invalid chunk terminator is not '\\r\\n': %r" % self.term
175
176 -class HeaderLimit(ParseException):
177 """ exception raised when we gore more headers than 178 max_header_count 179 """
180