Skip to content

Commit 41fbcb7

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #317 from wendell/pylons-raise
Reraise exception with original traceback
2 parents 41df3ab + 081ca98 commit 41fbcb7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

ddtrace/contrib/pylons/middleware.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import sys
23

34
from ...ext import http
45
from ...ext import AppTypes
@@ -39,8 +40,8 @@ def _start_response(status, *args, **kwargs):
3940
try:
4041
return self.app(environ, _start_response)
4142
except Exception as e:
42-
# "unexpected errors"
43-
# exc_info set by __exit__ on current tracer
43+
# store current exceptions info so we can re-raise it later
44+
(typ, val, tb) = sys.exc_info()
4445

4546
# e.code can either be a string or an int
4647
code = getattr(e, 'code', 500)
@@ -52,7 +53,9 @@ def _start_response(status, *args, **kwargs):
5253
code = 500
5354
span.set_tag(http.STATUS_CODE, code)
5455
span.error = 1
55-
raise e
56+
57+
# re-raise the original exception with its original traceback
58+
raise typ, val, tb
5659
except SystemExit:
5760
span.set_tag(http.STATUS_CODE, 500)
5861
span.error = 1

tests/contrib/pylons/test_pylons.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def test_pylons():
7777
eq_(s.meta.get(http.STATUS_CODE), '200')
7878

7979
def test_pylons_exceptions():
80+
# ensures the reported status code is 500 even if a wrong
81+
# status code is set and that the stacktrace points to the
82+
# right function
8083
writer = DummyWriter()
8184
tracer = Tracer()
8285
tracer.writer = writer
@@ -107,10 +110,10 @@ def test_pylons_exceptions():
107110
s = spans[0]
108111

109112
eq_(s.error, 1)
110-
eq_(s.get_tag("error.msg"), "Some exception")
111-
sc = int(s.get_tag("http.status_code"))
112-
eq_(sc, 500)
113-
ok_(s.get_tag("error.stack"))
113+
eq_(s.get_tag('error.msg'), 'Some exception')
114+
eq_(int(s.get_tag('http.status_code')), 500)
115+
ok_('start_response_exception' in s.get_tag('error.stack'))
116+
ok_('Exception: Some exception' in s.get_tag('error.stack'))
114117

115118
def test_pylons_string_code():
116119
writer = DummyWriter()

0 commit comments

Comments
 (0)