def process_traceback(info): return get_traceback( info=info, skip=1, show_hidden_frames=False, ignore_system_exceptions=True, )
def toolbar_handler(request): root_path = request.route_path("debugtoolbar.root") request.exc_history = exc_history remote_addr = request.remote_addr if request.path.startswith(root_path) or (not remote_addr in hosts): return handler(request) toolbar = DebugToolbar(request, panel_classes) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) except Exception: info = sys.exc_info() if exc_history is not None: tb = get_traceback(info=info, skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb body = tb.render_full(request, evalex=True).encode("utf-8", "replace") response = Response(body, status=500) toolbar.process_response(response) return response raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( "pyramid_debugtoolbar:templates/redirect.jinja2", {"redirect_to": redirect_to, "redirect_code": redirect_code}, ) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 toolbar.process_response(response) return response finally: # break circref del request.debug_toolbar
def toolbar_tween(request): root_path = request.route_path(ROOT_ROUTE_NAME) request.exc_history = exc_history remote_addr = request.remote_addr if remote_addr is None or request.path.startswith(root_path): return handler(request) else: for host in hosts: if ipaddr.IPAddress(remote_addr) in ipaddr.IPNetwork(host): break else: return handler(request) toolbar = DebugToolbar(request, panel_classes) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb body = tb.render_full(request).encode('utf-8', 'replace') response = Response(body, status=500) toolbar.process_response(response) qs = {'token':exc_history.token, 'tb':str(tb.id)} msg = 'Exception at %s\ntraceback url: %s' exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) exc_msg = msg % (request.url, exc_url) logger.exception(exc_msg) return response else: logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( 'pyramid_debugtoolbar:templates/redirect.dbtmako', {'redirect_to': redirect_to, 'redirect_code': redirect_code}, request=request) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 toolbar.process_response(response) return response finally: # break circref del request.debug_toolbar
def toolbar_tween(request): request.exc_history = exc_history request.history = request_history root_url = request.route_path('debugtoolbar', subpath='') exclude = [root_url] + exclude_prefixes last_proxy_addr = None try: p = request.path except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) starts_with_excluded = list(filter(None, map(p.startswith, exclude))) if request.remote_addr: last_proxy_addr = last_proxy(request.remote_addr) if last_proxy_addr is None \ or starts_with_excluded \ or not addr_in(last_proxy_addr, hosts) \ or auth_check and not auth_check(request): return handler(request) toolbar = DebugToolbar(request, panel_classes, global_panel_classes) request.debug_toolbar = toolbar _handler = handler # XXX for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) toolbar.status_int = response.status_int except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb request.pdbt_tb = tb qs = {'token': registry.pdtb_token, 'tb': str(tb.id)} msg = 'Exception at %s\ntraceback url: %s' exc_url = debug_toolbar_url(request, 'exception', _query=qs) exc_msg = msg % (request.url, exc_url) _logger.exception(exc_msg) subenviron = request.environ.copy() del subenviron['PATH_INFO'] del subenviron['QUERY_STRING'] subrequest = type(request).blank(exc_url, subenviron) subrequest.script_name = request.script_name subrequest.path_info = \ subrequest.path_info[len(request.script_name):] response = request.invoke_subrequest(subrequest) toolbar.process_response(request, response) request.id = hexlify(id(request)) toolbar.response = response toolbar.status_int = response.status_int request_history.put(request.id, toolbar) toolbar.inject(request, response) return response else: _logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( 'pyramid_debugtoolbar:templates/redirect.dbtmako', { 'redirect_to': redirect_to, 'redirect_code': redirect_code, }, request=request) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 toolbar.process_response(request, response) request.id = hexlify(id(request)) # Don't store the favicon.ico request # it's requested by the browser automatically if not "/favicon.ico" == request.path: toolbar.response = response request_history.put(request.id, toolbar) if not show_on_exc_only and response.content_type in html_types: toolbar.inject(request, response) return response finally: # break circref del request.debug_toolbar
def toolbar_tween(request): try: p = request.path_info except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) last_proxy_addr = None if request.remote_addr: last_proxy_addr = last_proxy(request.remote_addr) if ( last_proxy_addr is None or any(p.startswith(e) for e in exclude_prefixes) or not addr_in(last_proxy_addr, hosts) or auth_check and not auth_check(request) ): return handler(request) root_path = debug_toolbar_url(request, '', _app_url='') if p.startswith(root_path): # we know root_path will always have a trailing slash # but script_name doesn't want it try: old_script_name = request.script_name old_path_info = request.path_info request.script_name += root_path[:-1] request.path_info = request.path_info[len(root_path) - 1:] return dispatch(request) finally: request.script_name = old_script_name request.path_info = old_path_info request.exc_history = exc_history request.history = request_history request.pdtb_id = hexlify(id(request)) toolbar = DebugToolbar(request, panel_classes, global_panel_classes, default_active_panels) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) toolbar.status_int = response.status_int except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb request.pdbt_tb = tb msg = 'Exception at %s\ntraceback url: %s' qs = {'token': registry.pdtb_token, 'tb': str(tb.id)} subrequest = make_subrequest( request, root_path, 'exception', qs) exc_msg = msg % (request.url, subrequest.url) _logger.exception(exc_msg) response = dispatch(subrequest) # The original request must be processed so that the panel data exists # if the request is later examined in the full toolbar view. toolbar.process_response(request, response) toolbar.response = response toolbar.status_int = response.status_int request_history.put(request.pdtb_id, toolbar) # Inject the button to activate the full toolbar view. toolbar.inject(request, response) return response else: _logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: qs = { 'token': registry.pdtb_token, 'redirect_to': redirect_to, 'redirect_code': str(redirect_code), } subrequest = make_subrequest( request, root_path, 'redirect', qs) content = dispatch(subrequest).text response.location = None response.text = content response.status_int = 200 toolbar.process_response(request, response) # Don't store the favicon.ico request # it's requested by the browser automatically if not "/favicon.ico" == request.path: toolbar.response = response request_history.put(request.pdtb_id, toolbar) if not show_on_exc_only and response.content_type in html_types: toolbar.inject(request, response) return response finally: # break circref del request.debug_toolbar
def _do_route(self, action_name, method_name, params, metadata, trans_id, request): """ Performs routing, i.e. calls decorated methods/functions """ if params is None: params = list() settings = self.get_method(action_name, method_name) permission = settings.get('permission', None) ret = { "type": "rpc", "tid": trans_id, "action": action_name, "method": method_name, "result": None } callback = settings['callback'] append_request = settings.get('request_as_last_param', False) permission_ok = True context = request.root prepend = [] if settings['class']: instance = settings['class'](request) prepend.append(instance) context = instance elif append_request: params.append(request) if settings['metadata']: prepend.append(metadata) params = prepend + params if permission is not None: permission_ok = has_permission(permission, context, request) try: if not permission_ok: raise AccessDeniedException("Access denied") ret["result"] = callback(*params) except Exception as exc: ret["type"] = "exception" # Let a user defined view for specific exception prevent returning # a server error. exception_view = render_view_to_response(exc, request) if exception_view is not None: ret["result"] = exception_view return ret # Log Error LOG.error("%s: %s", str(exc.__class__.__name__), exc) LOG.info(traceback.format_exc()) if self.expose_exceptions: ret["result"] = { 'error': True, 'message': str(exc), 'exception_class': str(exc.__class__), 'stacktrace': traceback.format_exc() } else: message = 'Error executing {}.{}'.format(action_name, method_name) ret["result"] = { 'error': True, 'message': message } if self.debug_mode: # if pyramid_debugtoolbar is enabled, generate an interactive page # and include the url to access it in the ext direct Exception response text from pyramid_debugtoolbar.tbtools import get_traceback from pyramid_debugtoolbar.utils import EXC_ROUTE_NAME import sys exc_history = request.exc_history if exc_history is not None: tb = get_traceback( info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb qs = {'token': exc_history.token, 'tb': str(tb.id)} exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) ret['message'] = 'Exception: traceback url: {}'.format(exc_url) return ret
def toolbar_tween(request): root_path = request.route_path(ROOT_ROUTE_NAME) request.exc_history = exc_history remote_addr = request.remote_addr if (request.path.startswith(root_path) or (not remote_addr in hosts)): return handler(request) toolbar = DebugToolbar(request, panel_classes) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb body = tb.render_full(request).encode('utf-8', 'replace') response = Response(body, status=500) toolbar.process_response(response) qs = {'token': exc_history.token, 'tb': str(tb.id)} msg = 'Exception at %s\ntraceback url: %s' exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) exc_msg = msg % (request.url, exc_url) logger.exception(exc_msg) return response else: logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( 'pyramid_debugtoolbar:templates/redirect.mako', { 'redirect_to': redirect_to, 'redirect_code': redirect_code }, request=request) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 toolbar.process_response(response) return response finally: # break circref del request.debug_toolbar
def toolbar_tween(request): root_path = request.route_path(ROOT_ROUTE_NAME) exclude = [root_path] + exclude_prefixes request.exc_history = exc_history last_proxy_addr = None try: p = request.path except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) starts_with_excluded = list(filter(None, map(p.startswith, exclude))) if request.remote_addr: last_proxy_addr = last_proxy(request.remote_addr) if last_proxy_addr is None \ or starts_with_excluded \ or not addr_in(last_proxy_addr, hosts) \ or auth_check and not auth_check(request): return handler(request) toolbar = DebugToolbar(request, panel_classes) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb body = tb.render_full(request).encode('utf-8', 'replace') response = Response(body, status=500) toolbar.process_response(response) qs = {'token': exc_history.token, 'tb': str(tb.id)} msg = 'Exception at %s\ntraceback url: %s' exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) exc_msg = msg % (request.url, exc_url) logger.exception(exc_msg) return response else: logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( 'pyramid_debugtoolbar:templates/redirect.dbtmako', {'redirect_to': redirect_to, 'redirect_code': redirect_code}, request=request) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 if not show_on_exc_only: toolbar.process_response(response) return response finally: # break circref del request.debug_toolbar
def toolbar_tween(request): try: p = request.path_info except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) last_proxy_addr = None if request.remote_addr: last_proxy_addr = last_proxy(request.remote_addr) if (last_proxy_addr is None or any(p.startswith(e) for e in exclude_prefixes) or not addr_in(last_proxy_addr, hosts) or auth_check and not auth_check(request)): return handler(request) if request.environ.get('wsgi.multiprocess', False): warnings.warn( 'pyramid_debugtoolbar has detected that the application is ' 'being served by a forking / multiprocess web server. The ' 'toolbar relies on global state to work and is not compatible ' 'with this environment. The toolbar will be disabled.') return handler(request) root_path = debug_toolbar_url(request, '', _app_url='') if p.startswith(root_path): # we know root_path will always have a trailing slash # but script_name doesn't want it try: old_script_name = request.script_name old_path_info = request.path_info request.script_name += root_path[:-1] request.path_info = request.path_info[len(root_path) - 1:] return dispatch(request) finally: request.script_name = old_script_name request.path_info = old_path_info request.exc_history = exc_history request.history = request_history request.pdtb_id = hexlify(id(request)) toolbar = DebugToolbar(request, panel_classes, global_panel_classes, default_active_panels) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) toolbar.status_int = response.status_int except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb request.pdbt_tb = tb msg = 'Exception at %s\ntraceback url: %s' qs = {'token': registry.pdtb_token, 'tb': str(tb.id)} subrequest = make_subrequest(request, root_path, 'exception', qs) exc_msg = msg % (request.url, subrequest.url) _logger.exception(exc_msg) response = dispatch(subrequest) # The original request must be processed so that the panel data exists # if the request is later examined in the full toolbar view. toolbar.process_response(request, response) toolbar.response = response toolbar.status_int = response.status_int request_history.put(request.pdtb_id, toolbar) # Inject the button to activate the full toolbar view. toolbar.inject(request, response) return response else: _logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: qs = { 'token': registry.pdtb_token, 'redirect_to': redirect_to, 'redirect_code': str(redirect_code), } subrequest = make_subrequest(request, root_path, 'redirect', qs) content = dispatch(subrequest).text response.location = None response.text = content response.status_int = 200 toolbar.process_response(request, response) # Don't store the favicon.ico request # it's requested by the browser automatically if not "/favicon.ico" == request.path: toolbar.response = response request_history.put(request.pdtb_id, toolbar) if not show_on_exc_only and response.content_type in html_types: toolbar.inject(request, response) return response finally: # break circref del request.debug_toolbar
def _do_route(self, action_name, method_name, params, metadata, trans_id, request): """ Performs routing, i.e. calls decorated methods/functions """ if params is None: params = list() settings = self.get_method(action_name, method_name) permission = settings.get('permission', None) ret = { "type": "rpc", "tid": trans_id, "action": action_name, "method": method_name, "result": None } callback = settings['callback'] append_request = settings.get('request_as_last_param', False) permission_ok = True context = request.root prepend = [] if settings['class']: instance = settings['class'](request) prepend.append(instance) context = instance elif append_request: params.append(request) if settings['metadata']: prepend.append(metadata) params = prepend + params if permission is not None: permission_ok = has_permission(permission, context, request) try: if not permission_ok: raise AccessDeniedException("Access denied") ret["result"] = callback(*params) except Exception as exc: ret["type"] = "exception" # Let a user defined view for specific exception prevent returning # a server error. exception_view = render_view_to_response(exc, request) if exception_view is not None: ret["result"] = exception_view return ret # Log Error LOG.error("%s: %s", str(exc.__class__.__name__), exc) LOG.info(traceback.format_exc()) if self.expose_exceptions: ret["result"] = { 'error': True, 'message': str(exc), 'exception_class': str(exc.__class__), 'stacktrace': traceback.format_exc() } else: message = 'Error executing {}.{}'.format( action_name, method_name) ret["result"] = {'error': True, 'message': message} if self.debug_mode: # if pyramid_debugtoolbar is enabled, generate an interactive page # and include the url to access it in the ext direct Exception response text from pyramid_debugtoolbar.tbtools import get_traceback from pyramid_debugtoolbar.utils import EXC_ROUTE_NAME import sys exc_history = request.exc_history if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb qs = {'token': exc_history.token, 'tb': str(tb.id)} exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) ret['message'] = 'Exception: traceback url: {}'.format( exc_url) return ret
def toolbar_tween(request): root_path = request.route_path(ROOT_ROUTE_NAME) exclude = [root_path] + exclude_prefixes request.exc_history = exc_history last_proxy_addr = None try: p = request.path except UnicodeDecodeError as e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason) starts_with_excluded = list(filter(None, map(p.startswith, exclude))) if request.remote_addr: last_proxy_addr = last_proxy(request.remote_addr) if last_proxy_addr is None \ or starts_with_excluded \ or not addr_in(last_proxy_addr, hosts) \ or auth_check and not auth_check(request): return handler(request) toolbar = DebugToolbar(request, panel_classes) request.debug_toolbar = toolbar _handler = handler for panel in toolbar.panels: _handler = panel.wrap_handler(_handler) try: response = _handler(request) except Exception: if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb body = tb.render_full(request).encode('utf-8', 'replace') response = Response(body, status=500) toolbar.process_response(response) qs = {'token': exc_history.token, 'tb': str(tb.id)} msg = 'Exception at %s\ntraceback url: %s' exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) exc_msg = msg % (request.url, exc_url) _logger.exception(exc_msg) return response else: _logger.exception('Exception at %s' % request.url) raise else: if intercept_redirects: # Intercept http redirect codes and display an html page with a # link to the target. if response.status_int in redirect_codes: redirect_to = response.location redirect_code = response.status_int if redirect_to: content = render( 'pyramid_debugtoolbar:templates/redirect.dbtmako', { 'redirect_to': redirect_to, 'redirect_code': redirect_code }, request=request) content = content.encode(response.charset) response.content_length = len(content) response.location = None response.app_iter = [content] response.status_int = 200 if not show_on_exc_only: toolbar.process_response(response) return response finally: # break circref del request.debug_toolbar
def _do_route(self, action_name, method_name, params, trans_id, request): """ Perform routing, i.e. calls decorated methods/functions. """ if params is None: params = list() settings = self.get_method(action_name, method_name) permission = settings.get('permission', None) session_checks = settings.get('session_checks', True) ret = { 'type' : 'rpc', 'tid' : trans_id, 'action' : action_name, 'method' : method_name, 'sto' : request.user.sess_timeout if request.user else None, 'result' : None } callback = settings['callback'] append_request = settings.get('request_as_last_param', False) session_ok = True permission_ok = True if session_checks: if request.session.get('sess.pwage') in ('force', 'drop'): session_ok = False if hasattr(callback, '__self__'): if isinstance(callback.__self__, ExtModel): instance = callback.__self__ pinst = RootFactory(request) if permission is not None: permission_ok = has_permission(permission, pinst, request) else: permission_ok = has_permission('USAGE', pinst, request) if append_request: params.append(request) else: cls = callback.__self__.__class__ instance = cls(request) params.insert(0, instance) if permission is not None: permission_ok = has_permission(permission, instance, request) else: permission_ok = has_permission('USAGE', pinst, request) elif append_request: params.append(request) try: if (not permission_ok) or (not session_ok): raise AccessDeniedException('Access denied') ret['result'] = callback(*params) except Exception as e: # Let a user defined view for specific exception prevent returning # a server error. exception_view = render_view_to_response(e, request) if exception_view is not None: ret['result'] = exception_view return ret ret['type'] = 'exception' if self.expose_exceptions: ret['result'] = { 'error' : True, 'message' : str(e), 'exception_class' : str(e.__class__), 'stacktrace' : traceback.format_exc() } else: message = 'Error executing %s.%s' % (action_name, method_name) ret['result'] = { 'error' : True, 'message' : message } if self.debug_mode: # if pyramid_debugtoolbar is enabled, generate an interactive page # and include the url to access it in the ext direct Exception response text from pyramid_debugtoolbar.tbtools import get_traceback import sys exc_history = request.exc_history if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb qs = { 'tb' : str(tb.id), 'token' : request.registry.pdtb_token } msg = 'Exception: traceback URL: %s' exc_url = request.route_url('debugtoolbar', subpath=('exception',), _query=qs) exc_msg = msg % (exc_url) ret['message'] = exc_msg return ret
def _do_route(self, action_name, method_name, params, trans_id, request): """ Performs routing, i.e. calls decorated methods/functions """ if params is None: params = list() settings = self.get_method(action_name, method_name) permission = settings.get("permission", None) ret = {"type": "rpc", "tid": trans_id, "action": action_name, "method": method_name, "result": None} callback = settings["callback"] append_request = settings.get("request_as_last_param", False) permission_ok = True context = request.root if hasattr(callback, "im_class"): cls = callback.im_class instance = cls(request) params.insert(0, instance) context = instance elif append_request: params.append(request) if permission is not None: permission_ok = has_permission(permission, context, request) try: if not permission_ok: raise AccessDeniedException("Access denied") ret["result"] = callback(*params) except Exception, e: # Let a user defined view for specific exception prevent returning # a server error. exception_view = render_view_to_response(e, request) if exception_view is not None: ret["result"] = exception_view return ret ret["type"] = "exception" if self.expose_exceptions: ret["result"] = { "error": True, "message": str(e), "exception_class": str(e.__class__), "stacktrace": traceback.format_exc(), } else: message = "Error executing %s.%s" % (action_name, method_name) ret["result"] = {"error": True, "message": message} if self.debug_mode: # if pyramid_debugtoolbar is enabled, generate an interactive page # and include the url to access it in the ext direct Exception response text from pyramid_debugtoolbar.tbtools import get_traceback from pyramid_debugtoolbar.utils import EXC_ROUTE_NAME import sys exc_history = request.exc_history if exc_history is not None: tb = get_traceback( info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True ) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb qs = {"token": exc_history.token, "tb": str(tb.id)} msg = "Exception: traceback url: %s" exc_url = request.route_url(EXC_ROUTE_NAME, _query=qs) exc_msg = msg % (exc_url) ret["message"] = exc_msg
def _do_route(self, action_name, method_name, params, trans_id, request): """ Perform routing, i.e. calls decorated methods/functions. """ if params is None: params = list() settings = self.get_method(action_name, method_name) permission = settings.get('permission', None) session_checks = settings.get('session_checks', True) ret = { 'type': 'rpc', 'tid': trans_id, 'action': action_name, 'method': method_name, 'sto': request.user.sess_timeout if request.user else None, 'result': None } callback = settings['callback'] append_request = settings.get('request_as_last_param', False) session_ok = True if permission is not None: permission_ok = request.has_permission(permission) else: permission_ok = request.has_permission('USAGE') if session_checks: if request.session.get('sess.pwage') in ('force', 'drop'): session_ok = False if append_request: params.append(request) try: if (not permission_ok) or (not session_ok): raise AccessDeniedException('Access denied') ret['result'] = callback(*params) except Exception as e: # Let a user defined view for specific exception prevent returning # a server error. exception_view = None if not isinstance(e, AccessDeniedException): exception_view = render_view_to_response(e, request) if exception_view is not None: ret['result'] = exception_view return ret ret['type'] = 'exception' if self.expose_exceptions: ret['result'] = { 'error': True, 'message': str(e), 'exception_class': str(e.__class__), 'stacktrace': traceback.format_exc() } else: message = 'Error executing %s.%s' % (action_name, method_name) ret['result'] = {'error': True, 'message': message} if self.debug_mode: # if pyramid_debugtoolbar is enabled, generate an interactive page # and include the url to access it in the ext direct Exception response text from pyramid_debugtoolbar.tbtools import get_traceback import sys exc_history = request.exc_history if exc_history is not None: tb = get_traceback(info=sys.exc_info(), skip=1, show_hidden_frames=False, ignore_system_exceptions=True) for frame in tb.frames: exc_history.frames[frame.id] = frame exc_history.tracebacks[tb.id] = tb qs = { 'tb': str(tb.id), 'token': request.registry.pdtb_token } msg = 'Exception: traceback URL: %s' exc_url = request.route_url('debugtoolbar', subpath=('exception', ), _query=qs) exc_msg = msg % (exc_url) ret['message'] = exc_msg return ret