Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 719be9a

Browse files
ltmuntitaker
authored andcommitted
Use correct kwarg in handle_exception() for Flask (#1300)
* Use correct kwarg in handle_exception() for Flask The handle_exception() method is registered as a receiver of the got_request_exception signal. According to both the [documentation][1] and [source code][2] of Flask the got_request_exception signal passes the exception as `exception` rather than a `exc_info`. This is likely to have gone unnoticed since captureException() calls sys.exc_info() in the absence of an exception. On Python 3 we can use `__traceback__` to explicitly construct the `exc_info`. [1]: http://flask.pocoo.org/docs/1.0/api/#flask.got_request_exception [2]: https://github.com/pallets/flask/blob/1.0.2/flask/app.py#L1732 * doc: Add changelog
1 parent 6307373 commit 719be9a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
88
------
99

1010
* [Core] Fixed stackframes in some situations being in inverse order.
11+
* [Flask] Fix wrong exception handling logic (accidentally relied on Flask internals).
1112

1213
6.9.0 (2018-05-30)
1314
------------------

raven/contrib/flask.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ def handle_exception(self, *args, **kwargs):
136136
if not self.client:
137137
return
138138

139-
self.captureException(exc_info=kwargs.get('exc_info'))
139+
# got_request_exception signal passes the exception as 'exception'
140+
exception = kwargs.get('exception')
141+
if exception is not None and hasattr(exception, '__traceback__'):
142+
# On Python 3 we can contruct the exc_info via __traceback__
143+
exc_info = (type(exception), exception, exception.__traceback__)
144+
else:
145+
# The user may call the method with 'exc_info' manually
146+
exc_info = kwargs.get('exc_info')
147+
148+
self.captureException(exc_info=exc_info)
140149

141150
def get_user_info(self, request):
142151
"""

0 commit comments

Comments
 (0)