Skip to content

Commit b71a40e

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #401 from palazzem/pyramid-exceptions
Use pyramid HTTPExceptions as valid response types
2 parents 71f2e6d + ca57b47 commit b71a40e

File tree

7 files changed

+252
-186
lines changed

7 files changed

+252
-186
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ jobs:
449449
- restore_cache:
450450
keys:
451451
- tox-cache-pyramid-{{ checksum "tox.ini" }}
452-
- run: tox -e '{py27,py34,py35,py36}-pyramid{17,18}-webtest' --result-json /tmp/pyramid.1.results
453-
- run: tox -e '{py27,py34,py35,py36}-pyramid-autopatch{17,18}-webtest' --result-json /tmp/pyramid.2.results
452+
- run: tox -e '{py27,py34,py35,py36}-pyramid{17,18,19}-webtest' --result-json /tmp/pyramid.1.results
453+
- run: tox -e '{py27,py34,py35,py36}-pyramid-autopatch{17,18,19}-webtest' --result-json /tmp/pyramid.2.results
454454
- persist_to_workspace:
455455
root: /tmp
456456
paths:

ddtrace/contrib/pyramid/trace.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import pyramid.renderers
55
from pyramid.settings import asbool
6+
from pyramid.httpexceptions import HTTPException
67
import wrapt
78

89
# project
@@ -63,6 +64,14 @@ def trace_tween(request):
6364
response = None
6465
try:
6566
response = handler(request)
67+
except HTTPException as e:
68+
# If the exception is a pyramid HTTPException,
69+
# that's still valuable information that isn't necessarily
70+
# a 500. For instance, HTTPFound is a 302.
71+
# As described in docs, Pyramid exceptions are all valid
72+
# response types
73+
response = e
74+
raise
6675
except BaseException:
6776
span.set_tag(http.STATUS_CODE, 500)
6877
raise
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .web import create_app # noqa

tests/contrib/pyramid/app/web.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from ddtrace.contrib.pyramid import trace_pyramid
2+
3+
from pyramid.response import Response
4+
from pyramid.config import Configurator
5+
from pyramid.renderers import render_to_response
6+
from pyramid.httpexceptions import (
7+
HTTPInternalServerError,
8+
HTTPFound,
9+
HTTPNotFound,
10+
HTTPException,
11+
HTTPNoContent,
12+
)
13+
14+
15+
def create_app(settings, instrument):
16+
"""Return a pyramid wsgi app"""
17+
18+
def index(request):
19+
return Response('idx')
20+
21+
def error(request):
22+
raise HTTPInternalServerError("oh no")
23+
24+
def exception(request):
25+
1 / 0
26+
27+
def json(request):
28+
return {'a': 1}
29+
30+
def renderer(request):
31+
return render_to_response('template.pt', {'foo': 'bar'}, request=request)
32+
33+
def raise_redirect(request):
34+
raise HTTPFound()
35+
36+
def raise_no_content(request):
37+
raise HTTPNoContent()
38+
39+
def custom_exception_view(context, request):
40+
"""Custom view that forces a HTTPException when no views
41+
are found to handle given request
42+
"""
43+
if 'raise_exception' in request.url:
44+
raise HTTPNotFound()
45+
else:
46+
return HTTPNotFound()
47+
48+
config = Configurator(settings=settings)
49+
config.add_route('index', '/')
50+
config.add_route('error', '/error')
51+
config.add_route('exception', '/exception')
52+
config.add_route('json', '/json')
53+
config.add_route('renderer', '/renderer')
54+
config.add_route('raise_redirect', '/redirect')
55+
config.add_route('raise_no_content', '/nocontent')
56+
config.add_view(index, route_name='index')
57+
config.add_view(error, route_name='error')
58+
config.add_view(exception, route_name='exception')
59+
config.add_view(json, route_name='json', renderer='json')
60+
config.add_view(renderer, route_name='renderer', renderer='template.pt')
61+
config.add_view(raise_redirect, route_name='raise_redirect')
62+
config.add_view(raise_no_content, route_name='raise_no_content')
63+
# required to reproduce a regression test
64+
config.add_notfound_view(custom_exception_view)
65+
# required for rendering tests
66+
renderer = config.testing_add_renderer('template.pt')
67+
68+
if instrument:
69+
trace_pyramid(config)
70+
71+
return config.make_wsgi_app(), renderer

0 commit comments

Comments
 (0)