1 | n/a | """Exception classes raised by urllib. |
---|
2 | n/a | |
---|
3 | n/a | The base exception class is URLError, which inherits from OSError. It |
---|
4 | n/a | doesn't define any behavior of its own, but is the base class for all |
---|
5 | n/a | exceptions defined in this package. |
---|
6 | n/a | |
---|
7 | n/a | HTTPError is an exception class that is also a valid HTTP response |
---|
8 | n/a | instance. It behaves this way because HTTP protocol errors are valid |
---|
9 | n/a | responses, with a status code, headers, and a body. In some contexts, |
---|
10 | n/a | an application may want to handle an exception like a regular |
---|
11 | n/a | response. |
---|
12 | n/a | """ |
---|
13 | n/a | |
---|
14 | n/a | import urllib.response |
---|
15 | n/a | |
---|
16 | n/a | __all__ = ['URLError', 'HTTPError', 'ContentTooShortError'] |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | # do these error classes make sense? |
---|
20 | n/a | # make sure all of the OSError stuff is overridden. we just want to be |
---|
21 | n/a | # subtypes. |
---|
22 | n/a | |
---|
23 | n/a | class URLError(OSError): |
---|
24 | n/a | # URLError is a sub-type of OSError, but it doesn't share any of |
---|
25 | n/a | # the implementation. need to override __init__ and __str__. |
---|
26 | n/a | # It sets self.args for compatibility with other EnvironmentError |
---|
27 | n/a | # subclasses, but args doesn't have the typical format with errno in |
---|
28 | n/a | # slot 0 and strerror in slot 1. This may be better than nothing. |
---|
29 | n/a | def __init__(self, reason, filename=None): |
---|
30 | n/a | self.args = reason, |
---|
31 | n/a | self.reason = reason |
---|
32 | n/a | if filename is not None: |
---|
33 | n/a | self.filename = filename |
---|
34 | n/a | |
---|
35 | n/a | def __str__(self): |
---|
36 | n/a | return '<urlopen error %s>' % self.reason |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | class HTTPError(URLError, urllib.response.addinfourl): |
---|
40 | n/a | """Raised when HTTP error occurs, but also acts like non-error return""" |
---|
41 | n/a | __super_init = urllib.response.addinfourl.__init__ |
---|
42 | n/a | |
---|
43 | n/a | def __init__(self, url, code, msg, hdrs, fp): |
---|
44 | n/a | self.code = code |
---|
45 | n/a | self.msg = msg |
---|
46 | n/a | self.hdrs = hdrs |
---|
47 | n/a | self.fp = fp |
---|
48 | n/a | self.filename = url |
---|
49 | n/a | # The addinfourl classes depend on fp being a valid file |
---|
50 | n/a | # object. In some cases, the HTTPError may not have a valid |
---|
51 | n/a | # file object. If this happens, the simplest workaround is to |
---|
52 | n/a | # not initialize the base classes. |
---|
53 | n/a | if fp is not None: |
---|
54 | n/a | self.__super_init(fp, hdrs, url, code) |
---|
55 | n/a | |
---|
56 | n/a | def __str__(self): |
---|
57 | n/a | return 'HTTP Error %s: %s' % (self.code, self.msg) |
---|
58 | n/a | |
---|
59 | n/a | def __repr__(self): |
---|
60 | n/a | return '<HTTPError %s: %r>' % (self.code, self.msg) |
---|
61 | n/a | |
---|
62 | n/a | # since URLError specifies a .reason attribute, HTTPError should also |
---|
63 | n/a | # provide this attribute. See issue13211 for discussion. |
---|
64 | n/a | @property |
---|
65 | n/a | def reason(self): |
---|
66 | n/a | return self.msg |
---|
67 | n/a | |
---|
68 | n/a | @property |
---|
69 | n/a | def headers(self): |
---|
70 | n/a | return self.hdrs |
---|
71 | n/a | |
---|
72 | n/a | @headers.setter |
---|
73 | n/a | def headers(self, headers): |
---|
74 | n/a | self.hdrs = headers |
---|
75 | n/a | |
---|
76 | n/a | |
---|
77 | n/a | class ContentTooShortError(URLError): |
---|
78 | n/a | """Exception raised when downloaded size does not match content-length.""" |
---|
79 | n/a | def __init__(self, message, content): |
---|
80 | n/a | URLError.__init__(self, message) |
---|
81 | n/a | self.content = content |
---|