|
3 | 3 | import asyncio |
4 | 4 | import threading |
5 | 5 | from http import HTTPStatus |
6 | | -from typing import NoReturn |
| 6 | +from typing import TYPE_CHECKING, NoReturn |
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 | from pyqwest import ( |
|
19 | 19 | ) |
20 | 20 | from pyqwest.testing import ASGITransport, WSGITransport |
21 | 21 |
|
| 22 | +from connectrpc._protocol import HTTPException |
22 | 23 | from connectrpc.code import Code |
23 | 24 | from connectrpc.errors import ConnectError |
24 | 25 |
|
|
32 | 33 | ) |
33 | 34 | from .haberdasher_pb2 import Hat, Size |
34 | 35 |
|
| 36 | +if TYPE_CHECKING: |
| 37 | + from connectrpc.request import RequestContext |
| 38 | + |
35 | 39 | _errors = [ |
36 | 40 | (Code.CANCELED, "Operation was cancelled", 499), |
37 | 41 | (Code.UNKNOWN, "An unknown error occurred", 500), |
@@ -424,3 +428,148 @@ async def make_hat(self, request, ctx) -> NoReturn: |
424 | 428 | assert exc_info.value.code == Code.DEADLINE_EXCEEDED |
425 | 429 | assert exc_info.value.message == "Request timed out" |
426 | 430 | assert recorded_timeout_header == "200" |
| 431 | + |
| 432 | + |
| 433 | +@pytest.mark.asyncio |
| 434 | +async def test_async_unhandled_exception_reraised() -> None: |
| 435 | + class RaisingHaberdasher(Haberdasher): |
| 436 | + async def make_hat(self, request, ctx) -> NoReturn: |
| 437 | + raise TypeError("Something went wrong") |
| 438 | + |
| 439 | + app = HaberdasherASGIApplication(RaisingHaberdasher()) |
| 440 | + transport = ASGITransport(app) |
| 441 | + http_client = Client(transport) |
| 442 | + |
| 443 | + async with HaberdasherClient( |
| 444 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 445 | + ) as client: |
| 446 | + with pytest.raises(ConnectError, match="Something went wrong"): |
| 447 | + await client.make_hat(request=Size(inches=10)) |
| 448 | + |
| 449 | + assert isinstance(transport.app_exception, TypeError) |
| 450 | + assert str(transport.app_exception) == "Something went wrong" |
| 451 | + |
| 452 | + |
| 453 | +@pytest.mark.asyncio |
| 454 | +async def test_async_unhandled_exception_reraised_stream() -> None: |
| 455 | + class RaisingHaberdasher(Haberdasher): |
| 456 | + def make_similar_hats(self, request: Size, ctx: RequestContext) -> NoReturn: |
| 457 | + raise TypeError("Something went wrong") |
| 458 | + |
| 459 | + app = HaberdasherASGIApplication(RaisingHaberdasher()) |
| 460 | + transport = ASGITransport(app) |
| 461 | + http_client = Client(transport) |
| 462 | + |
| 463 | + async with HaberdasherClient( |
| 464 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 465 | + ) as client: |
| 466 | + with pytest.raises(ConnectError, match="Something went wrong"): |
| 467 | + async for _ in client.make_similar_hats(request=Size(inches=10)): |
| 468 | + pass |
| 469 | + |
| 470 | + assert isinstance(transport.app_exception, TypeError) |
| 471 | + assert str(transport.app_exception) == "Something went wrong" |
| 472 | + |
| 473 | + |
| 474 | +@pytest.mark.asyncio |
| 475 | +async def test_async_connect_exception_not_reraised() -> None: |
| 476 | + class RaisingHaberdasher(Haberdasher): |
| 477 | + async def make_hat(self, request, ctx) -> NoReturn: |
| 478 | + raise ConnectError(Code.INTERNAL, "We're broken") |
| 479 | + |
| 480 | + app = HaberdasherASGIApplication(RaisingHaberdasher()) |
| 481 | + transport = ASGITransport(app) |
| 482 | + http_client = Client(transport) |
| 483 | + |
| 484 | + async with HaberdasherClient( |
| 485 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 486 | + ) as client: |
| 487 | + with pytest.raises(ConnectError, match="We're broken"): |
| 488 | + await client.make_hat(request=Size(inches=10)) |
| 489 | + |
| 490 | + assert transport.app_exception is None |
| 491 | + |
| 492 | + |
| 493 | +@pytest.mark.asyncio |
| 494 | +async def test_async_connect_exception_not_reraised_stream() -> None: |
| 495 | + class RaisingHaberdasher(Haberdasher): |
| 496 | + def make_similar_hats(self, request: Size, ctx: RequestContext) -> NoReturn: |
| 497 | + raise ConnectError(Code.INTERNAL, "We're broken") |
| 498 | + |
| 499 | + app = HaberdasherASGIApplication(RaisingHaberdasher()) |
| 500 | + transport = ASGITransport(app) |
| 501 | + http_client = Client(transport) |
| 502 | + |
| 503 | + async with HaberdasherClient( |
| 504 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 505 | + ) as client: |
| 506 | + with pytest.raises(ConnectError, match="We're broken"): |
| 507 | + async for _ in client.make_similar_hats(request=Size(inches=10)): |
| 508 | + pass |
| 509 | + |
| 510 | + assert transport.app_exception is None |
| 511 | + |
| 512 | + |
| 513 | +@pytest.mark.asyncio |
| 514 | +async def test_async_http_exception_not_reraised() -> None: |
| 515 | + class RaisingHaberdasher(Haberdasher): |
| 516 | + async def make_hat(self, request, ctx) -> NoReturn: |
| 517 | + raise HTTPException(status=HTTPStatus.INTERNAL_SERVER_ERROR, headers=[]) |
| 518 | + |
| 519 | + app = HaberdasherASGIApplication(RaisingHaberdasher()) |
| 520 | + transport = ASGITransport(app) |
| 521 | + http_client = Client(transport) |
| 522 | + |
| 523 | + async with HaberdasherClient( |
| 524 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 525 | + ) as client: |
| 526 | + with pytest.raises(ConnectError, match="Internal Server Error"): |
| 527 | + await client.make_hat(request=Size(inches=10)) |
| 528 | + |
| 529 | + assert transport.app_exception is None |
| 530 | + |
| 531 | + |
| 532 | +def test_sync_unhandled_exception_logged() -> None: |
| 533 | + class RaisingHaberdasher(HaberdasherSync): |
| 534 | + def make_hat(self, request, ctx) -> NoReturn: |
| 535 | + raise TypeError("Something went wrong") |
| 536 | + |
| 537 | + app = HaberdasherWSGIApplication(RaisingHaberdasher()) |
| 538 | + transport = WSGITransport(app) |
| 539 | + http_client = SyncClient(transport) |
| 540 | + |
| 541 | + with ( |
| 542 | + HaberdasherClientSync( |
| 543 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 544 | + ) as client, |
| 545 | + pytest.raises(ConnectError, match="Something went wrong"), |
| 546 | + ): |
| 547 | + client.make_hat(request=Size(inches=10)) |
| 548 | + |
| 549 | + logged_error = transport.error_stream.getvalue() |
| 550 | + assert "Exception in WSGI application" in logged_error |
| 551 | + assert "TypeError: Something went wrong" in logged_error |
| 552 | + assert "Traceback" in logged_error |
| 553 | + |
| 554 | + |
| 555 | +def test_sync_unhandled_exception_logged_stream() -> None: |
| 556 | + class RaisingHaberdasher(HaberdasherSync): |
| 557 | + def make_similar_hats(self, request, ctx) -> NoReturn: |
| 558 | + raise TypeError("Something went wrong") |
| 559 | + |
| 560 | + app = HaberdasherWSGIApplication(RaisingHaberdasher()) |
| 561 | + transport = WSGITransport(app) |
| 562 | + http_client = SyncClient(transport) |
| 563 | + |
| 564 | + with ( |
| 565 | + HaberdasherClientSync( |
| 566 | + "http://localhost", timeout_ms=200, http_client=http_client |
| 567 | + ) as client, |
| 568 | + pytest.raises(ConnectError, match="Something went wrong"), |
| 569 | + ): |
| 570 | + next(client.make_similar_hats(request=Size(inches=10))) |
| 571 | + |
| 572 | + logged_error = transport.error_stream.getvalue() |
| 573 | + assert "Exception in WSGI application" in logged_error |
| 574 | + assert "TypeError: Something went wrong" in logged_error |
| 575 | + assert "Traceback" in logged_error |
0 commit comments