]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Show exceptions that occur during app init. Ignore non-http scopes. 31/head
authorTom Christie <tom@tomchristie.com>
Wed, 18 Jul 2018 14:34:13 +0000 (15:34 +0100)
committerTom Christie <tom@tomchristie.com>
Wed, 18 Jul 2018 14:34:13 +0000 (15:34 +0100)
starlette/debug.py
tests/test_debug.py

index d40c6d3f2a5ec45b51c01d5e87fc9be92648fc6d..eb1455a0eba51598fbde2b7f6a63ccef2b465aa8 100644 (file)
@@ -9,19 +9,22 @@ class DebugMiddleware:
         self.app = app
 
     def __call__(self, scope):
+        if scope["type"] != "http":
+            return self.app(scope)
         return _DebugResponder(self.app, scope)
 
 
 class _DebugResponder:
     def __init__(self, app, scope):
+        self.app = app
         self.scope = scope
-        self.asgi_instance = app(scope)
         self.response_started = False
 
     async def __call__(self, receive, send):
         self.raw_send = send
         try:
-            await self.asgi_instance(receive, self.send)
+            asgi = self.app(self.scope)
+            await asgi(receive, self.send)
         except:
             if self.response_started:
                 raise
index 8694bb091547326c115aae74a779a67a799a77c5..fffeec938cbaad2a0e70b96e6f1ff28818f78b1f 100644 (file)
@@ -43,3 +43,25 @@ def test_debug_after_response_sent():
     client = TestClient(DebugMiddleware(app))
     with pytest.raises(RuntimeError):
         response = client.get("/")
+
+
+def test_debug_error_during_scope():
+    def app(scope):
+        raise RuntimeError("Something went wrong")
+
+    app = DebugMiddleware(app)
+    client = TestClient(DebugMiddleware(app))
+    response = client.get("/", headers={"Accept": "text/html, */*"})
+    assert response.status_code == 500
+    assert response.headers["content-type"].startswith("text/html")
+    assert "RuntimeError" in response.text
+
+
+def test_debug_not_http():
+    def app(scope):
+        raise RuntimeError("Something went wrong")
+
+    app = DebugMiddleware(app)
+
+    with pytest.raises(RuntimeError):
+        app({"type": "websocket"})