Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken exception handling introduced by PR #303 #305

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pyramid_debugtoolbar/panels/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def process_response(self, response):

# stop hanging onto the request after the response is processed
del self.request

def render_vars(self, request):
return {
'static_path': request.static_url(STATIC_PATH),
Expand All @@ -70,7 +69,7 @@ def __init__(self, request):
if exc_history is None:
raise HTTPBadRequest('No exception history')
self.exc_history = exc_history
token = self.request.params.get('token')
token = self.request.matchdict.get('token')
if not token:
raise HTTPBadRequest('No token in request')
if not token == request.registry.parent_registry.pdtb_token:
Expand Down Expand Up @@ -115,9 +114,9 @@ def execute(self):
return HTTPBadRequest()

def includeme(config):
config.add_route(EXC_ROUTE_NAME, '/exception')
config.add_route('debugtoolbar.source', '/source')
config.add_route('debugtoolbar.execute', '/execute')
config.add_route(EXC_ROUTE_NAME, '/exception/{token}')
config.add_route('debugtoolbar.source', '/source/{token}')
config.add_route('debugtoolbar.execute', '/execute/{token}')

config.add_debugtoolbar_panel(TracebackPanel)
config.scan(__name__)
4 changes: 2 additions & 2 deletions pyramid_debugtoolbar/tbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ def render_full(self, request, lodgeit_url=None):
exc = escape(self.exception)
summary = self.render_summary(include_title=False, request=request)
token = request.registry.parent_registry.pdtb_token
qs = {'token': token, 'tb': str(self.id)}
url = request.route_url(EXC_ROUTE_NAME, _query=qs)
qs = {'tb': str(self.id)}
url = request.route_url(EXC_ROUTE_NAME, token=token, _query=qs)
evalex = request.exc_history.eval_exc
vars = {
'evalex': evalex and 'true' or 'false',
Expand Down
9 changes: 6 additions & 3 deletions pyramid_debugtoolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ def sget(opt, default=None):

dispatch = lambda request: _dispatch(toolbar_app, request)

def toolbar_tween(request):
def append_token(path, token):
return path + '/' + token

def toolbar_tween(request, path_helper=append_token):
try:
p = request.path_info
except UnicodeDecodeError as e:
Expand Down Expand Up @@ -254,9 +257,9 @@ def toolbar_tween(request):
request.pdbt_tb = tb

msg = 'Exception at %s\ntraceback url: %s'
qs = {'token': registry.pdtb_token, 'tb': str(tb.id)}
qs = {'tb': str(tb.id)}
subrequest = make_subrequest(
request, root_path, 'exception', qs)
request, root_path, path_helper('exception', registry.pdtb_token), qs)
exc_msg = msg % (request.url, subrequest.url)
_logger.exception(exc_msg)

Expand Down
11 changes: 6 additions & 5 deletions tests/test_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def _makeExceptionDispatcher(self):
registry = self.config.registry
def dispatcher(app, request):
request.registry = registry.parent_registry = registry
request.matchdict = {'token': request.registry.parent_registry.pdtb_token}
request.exc_history = registry.exc_history
return ExceptionDebugView(request).exception()
return dispatcher
Expand Down Expand Up @@ -294,7 +295,7 @@ def handler(request):
raise NotImplementedError
self.config.registry.settings['debugtoolbar.intercept_exc'] = True
self.config.registry.settings['debugtoolbar.secret'] = 'abc'
self.config.add_route('debugtoolbar.exception', '/exception')
self.config.add_route('debugtoolbar.exception', '/exception/{token}')
request.registry = self.config.registry
request.remote_addr = '127.0.0.1'
logger = DummyLogger()
Expand Down Expand Up @@ -333,7 +334,7 @@ def handler(request):
raise NotImplementedError(b'K\xc3\xa4se!\xe2\x98\xa0')
self.config.registry.settings['debugtoolbar.intercept_exc'] = True
self.config.registry.settings['debugtoolbar.secret'] = 'abc'
self.config.add_route('debugtoolbar.exception', '/exception')
self.config.add_route('debugtoolbar.exception', '/exception/{token}')
request.registry = self.config.registry
request.remote_addr = '127.0.0.1'
logger = DummyLogger()
Expand All @@ -353,7 +354,7 @@ def handler(request):
self.config.registry.settings['debugtoolbar.show_on_exc_only'] = True
self.config.registry.settings['debugtoolbar.intercept_exc'] = True
self.config.registry.settings['debugtoolbar.secret'] = 'abc'
self.config.add_route('debugtoolbar.exception', '/exception')
self.config.add_route('debugtoolbar.exception', '/exception/{token}')
request.registry = self.config.registry
request.remote_addr = '127.0.0.1'
logger = DummyLogger()
Expand All @@ -371,7 +372,7 @@ def handler(request):
self.config.registry.settings['debugtoolbar.show_on_exc_only'] = True
self.config.registry.settings['debugtoolbar.intercept_exc'] = True
self.config.registry.settings['debugtoolbar.secret'] = 'abc'
self.config.add_route('debugtoolbar.exception', '/exception')
self.config.add_route('debugtoolbar.exception', '/exception/{token}')
request.registry = self.config.registry
request.remote_addr = '127.0.0.1'
response = self._callFUT(request, handler)
Expand All @@ -388,7 +389,7 @@ def handler(request):
self.config.registry.settings['debugtoolbar.show_on_exc_only'] = False
self.config.registry.settings['debugtoolbar.intercept_exc'] = True
self.config.registry.settings['debugtoolbar.secret'] = 'abc'
self.config.add_route('debugtoolbar.exception', '/exception')
self.config.add_route('debugtoolbar.exception', '/exception/{token}')
request.registry = self.config.registry
request.remote_addr = '127.0.0.1'
response = self._callFUT(request, handler)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _makeOne(self, request):
def _makeRequest(self):
request = testing.DummyRequest()
request.secret = 'abc';
request.params['token'] = 'token'
request.matchdict['token'] = 'token'
request.exc_history = self._makeExceptionHistory()
return request

Expand All @@ -46,13 +46,12 @@ def test_no_exc_history(self):
def test_without_token_in_request(self):
from pyramid.httpexceptions import HTTPBadRequest
request = self._makeRequest()
del request.params['token']
del request.matchdict['token']
self.assertRaises(HTTPBadRequest, self._makeOne, request)

def test_with_bad_token_in_request(self):
from pyramid.httpexceptions import HTTPBadRequest
request = self._makeRequest()
request.params['token'] = 'wrong'
request.matchdict['token'] = 'wrong'
self.assertRaises(HTTPBadRequest, self._makeOne, request)

def test_source(self):
Expand Down