From 224b484195295fbebfbdbfafa570f1df1ce839fa Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 21 Mar 2024 14:24:07 -0700 Subject: [PATCH] test_classes: Skip OpenAPI validation on exceptions. This gives more helpful error tracebacks from failing tests. Signed-off-by: Anders Kaseorg --- zerver/lib/test_classes.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 86eeea509d..8d85a1b3cd 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -32,6 +32,7 @@ import responses from django.apps import apps from django.conf import settings from django.core.mail import EmailMessage +from django.core.signals import got_request_exception from django.db import connection from django.db.migrations.executor import MigrationExecutor from django.db.migrations.state import StateApps @@ -152,10 +153,23 @@ class UploadSerializeMixin(SerializeMixin): class ZulipClientHandler(ClientHandler): @override def get_response(self, request: HttpRequest) -> HttpResponseBase: + got_exception = False + + def on_exception(**kwargs: object) -> None: + nonlocal got_exception + if kwargs["request"] is request: + got_exception = True + request.body # noqa: B018 # prevents RawPostDataException - response = super().get_response(request) + got_request_exception.connect(on_exception) + try: + response = super().get_response(request) + finally: + got_request_exception.disconnect(on_exception) + if ( - request.method != "OPTIONS" + not got_exception # Django will reraise this exception + and request.method != "OPTIONS" and isinstance(response, HttpResponse) and not ( response.status_code == 302 and response.headers["Location"].startswith("/login/")