def response_error(code, headers=None): html = { 32:400, 33:403, 34:404, 44:405, 64:403, 88:402, 89:406, 92:500, 130:503, } if headers: response.headerlist += headers response.status_code = html[code] result = { 'errors': [ { 'title':get_exception( html[code] ).title, 'message':get_exception( html[code] ).explanation, 'code': code } ] } return result
def redirect_to(*args, **kargs): """Raises a redirect exception Optionally, a _code variable may be passed with the status code of the redirect, ie: .. code-block:: Python redirect_to('home_page', _code=303) ``Deprecated`` A Response object can be passed in as _response which will have the headers and cookies extracted from it and added into the redirect issued.""" response = kargs.pop("_response", None) status_code = kargs.pop("_code", 302) exc = httpexceptions.get_exception(status_code) found = exc(url_for(*args, **kargs)) log.debug("Generating %s redirect" % status_code) if response: warnings.warn(pylons.legacy.redirect_response_warning, PendingDeprecationWarning, 2) log.debug("Merging provided Response object into redirect") if str(response.status_code).startswith("3"): found.code = response.status_code for c in response.cookies.values(): found.headers.add("Set-Cookie", c.output(header="")) raise found
def redirect_to(*args, **kargs): """Raises a redirect exception Optionally, a _code variable may be passed with the status code of the redirect, ie: .. code-block:: Python redirect_to('home_page', _code=303) ``Deprecated`` A Response object can be passed in as _response which will have the headers and cookies extracted from it and added into the redirect issued.""" response = kargs.pop('_response', None) status_code = kargs.pop('_code', 302) exc = httpexceptions.get_exception(status_code) found = exc(url_for(*args, **kargs)) log.debug("Generating %s redirect" % status_code) if response: warnings.warn(pylons.legacy.redirect_response_warning, PendingDeprecationWarning, 2) log.debug("Merging provided Response object into redirect") if str(response.status_code).startswith('3'): found.code = response.status_code for c in response.cookies.values(): found.headers.add('Set-Cookie', c.output(header='')) raise found
def wsgi_status( self ): """ Return status line in format appropriate for WSGI `start_response` """ if isinstance( self.status, int ): exception = httpexceptions.get_exception( self.status ) return "%d %s" % ( exception.code, exception.title ) else: return self.status
def abort(status_code=None, detail="", headers=None, comment=None): """Aborts the request immediately by returning an HTTP exception In the event that the status_code is a 300 series error, the detail attribute will be used as the Location header should one not be specified in the headers attribute. """ exc = httpexceptions.get_exception(status_code)(detail, headers, comment) log.debug( "Aborting request, status: %s, detail: %r, headers: %r, " "comment: %r", status_code, detail, headers, comment ) raise exc
def abort(status_code=None, detail="", headers=None, comment=None): """Aborts the request immediately by returning an HTTP exception In the event that the status_code is a 300 series error, the detail attribute will be used as the Location header should one not be specified in the headers attribute. """ exc = httpexceptions.get_exception(status_code)(detail, headers, comment) log.debug( "Aborting request, status: %s, detail: %r, headers: %r, " "comment: %r", status_code, detail, headers, comment) raise exc
def __init__(self, applications, catch=(404, )): self.apps = applications self.catch_codes = {} self.catch_exceptions = [] for error in catch: if isinstance(error, str): error = int(error.split(None, 1)[0]) if isinstance(error, httpexceptions.HTTPException): exc = error code = error.code else: exc = httpexceptions.get_exception(error) code = error self.catch_codes[code] = exc self.catch_exceptions.append(exc) self.catch_exceptions = tuple(self.catch_exceptions)
def __init__(self, applications, catch=(404,)): self.apps = applications self.catch_codes = {} self.catch_exceptions = [] for error in catch: if isinstance(error, str): error = int(error.split(None, 1)[0]) if isinstance(error, httpexceptions.HTTPException): exc = error code = error.code else: exc = httpexceptions.get_exception(error) code = error self.catch_codes[code] = exc self.catch_exceptions.append(exc) self.catch_exceptions = tuple(self.catch_exceptions)
def abort(code_or_exception=None, detail="", headers=None, comment=None): """Raise an HTTPException and save it in environ for use by error pages.""" # Pylons 0.9.6 makes it really hard to get your raised HTTPException, # so this helper implements it manually using a familiar syntax. # FIXME: when we upgrade Pylons, we can replace this with raise # and access environ['pylons.controller.exception'] if isinstance(code_or_exception, httpexceptions.HTTPException): exc = code_or_exception else: if type(code_or_exception) is type and issubclass(code_or_exception, httpexceptions.HTTPException): exc_cls = code_or_exception else: exc_cls = httpexceptions.get_exception(code_or_exception) exc = exc_cls(detail, headers, comment) request.environ['r2.controller.exception'] = exc raise exc
def _get_response_from_code(self, http_code): try: return get_exception(safe_int(http_code)) except Exception: log.exception('Failed to fetch response for code %s' % http_code) return HTTPForbidden
def use_redirect(self): pylons.response.set_cookie('message', 'Hello World') exc = httpexceptions.get_exception(301) raise exc('/elsewhere')