codekingpro/portable-devtools
114k
1"""Exception classes raised by urllib.2 3The base exception class is URLError, which inherits from OSError. It4doesn't define any behavior of its own, but is the base class for all5exceptions defined in this package.6 7HTTPError is an exception class that is also a valid HTTP response8instance. It behaves this way because HTTP protocol errors are valid9responses, with a status code, headers, and a body. In some contexts,10an application may want to handle an exception like a regular11response.12"""13import io14import urllib.response15 16__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']17 18 19class URLError(OSError):20 # URLError is a sub-type of OSError, but it doesn't share any of21 # the implementation. need to override __init__ and __str__.22 # It sets self.args for compatibility with other OSError23 # subclasses, but args doesn't have the typical format with errno in24 # slot 0 and strerror in slot 1. This may be better than nothing.25 def __init__(self, reason, filename=None):26 self.args = reason,27 self.reason = reason28 if filename is not None:29 self.filename = filename30 31 def __str__(self):32 return '<urlopen error %s>' % self.reason33 34 35class HTTPError(URLError, urllib.response.addinfourl):36 """Raised when HTTP error occurs, but also acts like non-error return"""37 __super_init = urllib.response.addinfourl.__init__38 39 def __init__(self, url, code, msg, hdrs, fp):40 self.code = code41 self.msg = msg42 self.hdrs = hdrs43 self.fp = fp44 self.filename = url45 if fp is None:46 fp = io.BytesIO()47 self.__super_init(fp, hdrs, url, code)48 49 def __str__(self):50 return 'HTTP Error %s: %s' % (self.code, self.msg)51 52 def __repr__(self):53 return '<HTTPError %s: %r>' % (self.code, self.msg)54 55 # since URLError specifies a .reason attribute, HTTPError should also56 # provide this attribute. See issue13211 for discussion.57 @property58 def reason(self):59 return self.msg60 61 @property62 def headers(self):63 return self.hdrs64 65 @headers.setter66 def headers(self, headers):67 self.hdrs = headers68 69 70class ContentTooShortError(URLError):71 """Exception raised when downloaded size does not match content-length."""72 def __init__(self, message, content):73 URLError.__init__(self, message)74 self.content = content75 