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