classAborter(object):""" When passed a dict of code -> exception items it can be used as callable that raises exceptions. If the first argument to the callable is an integer it will be looked up in the mapping, if it's a WSGI application it will be raised in a proxy exception. The rest of the arguments are forwarded to the exception constructor. """def__init__(self,mapping=None,extra=None):if mapping isNone: mapping = default_exceptions self.mapping =dict(mapping)if extra isnotNone: self.mapping.update(extra)def__call__(self,code,*args,**kwargs):ifnot args andnot kwargs andnotisinstance(code, integer_types):raiseHTTPException(response=code)if code notin self.mapping:raiseLookupError('no exception for %r'% code)raise self.mapping[code](*args, **kwargs)
classNotFound(HTTPException):"""*404* `Not Found` Raise if a resource does not exist and never existed. """ code =404 description = ('The requested URL was not found on the server. ''If you entered the URL manually please check your spelling and ''try again.' )
# get_response最终返回了一个Response对象defget_response(self,environ=None):"""Get a response object. If one was passed to the exception it's returned directly. :param environ: the optional environ for the request. This can be used to modify the response depending on how the request looked like. :return: a :class:`Response` object or a subclass thereof. """if self.response isnotNone:return self.responseif environ isnotNone: environ =_get_environ(environ) headers = self.get_headers(environ)# get_body获取页面文本内容returnResponse(self.get_body(environ), self.code, headers)
get_body()
defget_body(self,environ=None):"""Get the HTML body."""returntext_type((u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n'u'<title>%(code)s%(name)s</title>\n'u'<h1>%(name)s</h1>\n'u'%(description)s\n' ) % {'code': self.code,'name': escape(self.name),# self.get_description获取异常的description信息'description': self.get_description(environ) })