Open maxcountryman opened 9 years ago
I'm experiencing the same problem. Also using tester to make a POST
@aop What I ended up doing was writing my own version of the middleware and using that directly. Everything has been fine since.
@maxcountryman Could you expand on this? I'm running into the same problem (though, using a DELETE), and I'm lost
Unfortunately, I don't have the time to dig through Flask to fix it by myself. If someone can come with a PR I'd be happy to include it.
@xordoquy it's a bug in raven-python (or rather in the Flask extension therein) but I don't really have time to track it down. I just ended up writing my own minimal middleware. Specifically my middleware only implements the try/catch logic, invoking Sentry--that's good enough for my purposes.
class SentryMiddleware(raven.middleware.Sentry):
def __call__(self, environ, start_response):
self.client.http_context(self.get_http_context(environ))
try:
return self.application(environ, start_response)
except Exception:
self.client.captureException()
raise
finally:
self.client.context.clear()
I'm no longer sure my error stems from the same issue, but I did figure out what was giving me grief, and maybe someone will benefit from an explanation: I was returning a 204, which has no response body. Thus, when next(app_iter)
was run by werkzeug, it had nothing left to iterate, and the StopIteration
bubbled up. My solution was to pass buffered=True
(using a custom flask.testing.FlaskClient
), which doesn't call next()
directly, and avoids that problem.
@theY4Kman ah, that probably explains my issue too.
@maxcountryman what I meant is I don't have the time to learn Flask to investigate the Flask extension issue. PR welcomed !
I've confirmed this 204 issue, though I never connected it to Sentry. I'm wondering if this is actual an error with the flask test runner. I have a bunch of code with # XXX(dcramer): 204 break tests, so we return a 200
just because of this.
@dcramer The Flask test runner works fine sans Sentry.
@xordoquy Sorry, I also don't have the time, which is why I just threw together the simplest solution.
+1. Sentry was breaking one of my tests running DELETE, adding buffered=True solved the problem. Thanks @theY4Kman!
Yep, I'm coming across this too.
+1 Test failing on 204
response when sentry is present
+1 Ran into this while integrating sentry, fixed with buffered=True
This is because of sentry wrapping the default wsgi app with its own.
To disable this, you can either run your app in debug, init sentry with Sentry(app, wrap_wsgi=False)
or sentry.init_app(app, wrap_wsgi=False)
or re-override the wsgi_app using
from raven.middleware import Sentry
if isinstance(app.wsgi_app, Sentry):
app.wsgi_app = app.wsgi_app.application
The sentry middleware needs to be fixed in order to avoid such hacks.
FWIW this is still happening
test case to reproduce:
diff --git a/tests/contrib/flask/tests.py b/tests/contrib/flask/tests.py
index 0530216..7584ae5 100644
--- a/tests/contrib/flask/tests.py
+++ b/tests/contrib/flask/tests.py
@@ -286,6 +286,11 @@ class FlaskTest(BaseTest):
def test_check_client_type(self):
self.assertRaises(TypeError, lambda _: Sentry(self.app, "oops, I'm putting my DSN instead"))
+ def test_options_request(self):
+ # should not raise `StopIteration`
+ rv = self.client.options('/message/')
+ assert rv.status_code == 200
+
class FlaskLoginTest(BaseTest):
is it more likely a issue in werkzeug.test.run_wsgi_app
? ref: https://github.com/pallets/werkzeug/issues/618
It seems that the Flask extension is causing the Werkzeug test client to raise a
StopIteration
exception.Here's part of the traceback:
This only seems to happen for tests that use the test client and make POSTs. Interestingly if I use the Sentry middleware directly, I'm not able to reproduce the error leading me to believe the problem lies in the Flask extension. However, so far I haven't been able to isolate the exact cause.