deffirst_or_404(self):"""Like :meth:`first` but aborts with 404 if not found instead of returning ``None``.""" rv = self.first()if rv isNone:abort(404)return rv
abort()
defabort(status,*args,**kwargs):''' Raises an :py:exc:`HTTPException` for the given status code or WSGI application:: abort(404) # 404 Not Found abort(Response('Hello World')) Can be passed a WSGI application or a status code. If a status code is given it's looked up in the list of exceptions and will raise that exception, if passed a WSGI application it will wrap it in a proxy WSGI exception and raise that:: abort(404) abort(Response('Hello World')) '''return_aborter(status, *args, **kwargs)_aborter =Aborter()
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)