test_classes: Skip OpenAPI validation on exceptions.

This gives more helpful error tracebacks from failing tests.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-03-21 14:24:07 -07:00 committed by Tim Abbott
parent 1aaedf89ee
commit 224b484195
1 changed files with 16 additions and 2 deletions

View File

@ -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
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/")