def forbidden_error(context, request): @request.after def nocache(response): response.headers.add("Cache-Control", "no-store") if request.identity is NO_IDENTITY and not request.path.startswith( "/api/"): @request.after def forget(response): request.app.forget_identity(response, request) return morepath.redirect( request.relative_url("/login?came_from=%s" % urllib.parse.quote(request.url))) @request.after def adjust_status(response): response.status = 403 # FIXME: should log this when a config for DEBUG_SECURITY is enabled # logger.error(traceback.format_exc()) if request.path.startswith("/api/"): return morepath.render_json( { "status": "error", "message": "Access Denied : %s" % request.path }, request) else: render = request.app.config.template_engine_registry.get_template_render( "master/error_403.pt", morepath.render_html) return render({}, request)
def httpnotfound_error(context, request: morepath.Request): @request.after def adjust_status(response): response.status = 404 if request.path.startswith('/api/'): return morepath.render_json({ 'status': 'error', 'message': 'Object Not Found : %s' % request.path}, request) else: render = request.app.config.template_engine_registry.get_template_render( 'master/error_404.pt', morepath.render_html) return render({}, request)
def tween(request): try: response = handler(request) response.headers['Tween-Header'] = 'FOO' return response except RuntimeError: if reset_request: request.reset() response = render_json( { 'app': repr(type(request.app)), 'unconsumed': request.unconsumed }, request) response.status_code = 500 return response
def httpnotfound_error(context, request: morepath.Request): @request.after def adjust_status(response): response.status = 404 if request.path.startswith("/api/"): return morepath.render_json( { "status": "error", "message": "Object Not Found : %s" % request.path }, request, ) else: render = request.app.config.template_engine_registry.get_template_render( "master/error_404.pt", morepath.render_html) return render({}, request)
def tween(request): try: response = handler(request) response.headers["Tween-Header"] = "FOO" return response except RuntimeError: if reset_request: request.reset() response = render_json( { "app": repr(type(request.app)), "unconsumed": request.unconsumed, }, request, ) response.status_code = 500 return response
def internalserver_error(context, request): @request.after def adjust_status(response): response.status = 500 tb = traceback.format_exc() logger.error("Internal Server Error\n" + tb) if request.path.startswith("/api/"): return morepath.render_json( { "status": "error", "message": "Internal server error", "traceback": tb.split("\n"), }, request, ) else: render = request.app.config.template_engine_registry.get_template_render( "master/error_500.pt", morepath.render_html) formatter = HtmlFormatter() highlighted = highlight(tb, PythonTracebackLexer(), formatter) return render({"traceback": highlighted}, request)