| 1 | import pytest |
| 2 | from django.http import Http404 |
| 3 | |
| 4 | from ninja import NinjaAPI, Schema |
| 5 | from ninja.testing import TestAsyncClient, TestClient |
| 6 | |
| 7 | api = NinjaAPI() |
| 8 | |
| 9 | |
| 10 | class CustomException(Exception): |
| 11 | pass |
| 12 | |
| 13 | |
| 14 | @api.exception_handler(CustomException) |
| 15 | def on_custom_error(request, exc): |
| 16 | return api.create_response(request, {"custom": True}, status=422) |
| 17 | |
| 18 | |
| 19 | class Payload(Schema): |
| 20 | test: int |
| 21 | |
| 22 | |
| 23 | @api.post("/error/{code}") |
| 24 | def err_thrower(request, code: str, payload: Payload = None): |
| 25 | if code == "base": |
| 26 | raise RuntimeError("test") |
| 27 | if code == "404": |
| 28 | raise Http404("test") |
| 29 | if code == "custom": |
| 30 | raise CustomException("test") |
| 31 | |
| 32 | |
| 33 | client = TestClient(api) |
| 34 | |
| 35 | |
| 36 | def test_default_handler(settings): |
| 37 | settings.DEBUG = True |
| 38 | |
| 39 | response = client.post("/error/base") |
| 40 | assert response.status_code == 500 |
| 41 | assert b"RuntimeError: test" in response.content |
| 42 | |
| 43 | response = client.post("/error/404") |
| 44 | assert response.status_code == 404 |
| 45 | assert response.json() == {"detail": "Not Found: test"} |
| 46 | |
| 47 | response = client.post("/error/custom", body="invalid_json") |
| 48 | assert response.status_code == 400 |
| 49 | assert response.json() == { |
| 50 | "detail": "Cannot parse request body (Expecting value: line 1 column 1 (char 0))", |
| 51 | } |
| 52 | |
| 53 | settings.DEBUG = False |
| 54 | with pytest.raises(RuntimeError): |
| 55 | response = client.post("/error/base") |
| 56 | |
| 57 | response = client.post("/error/custom", body="invalid_json") |
| 58 | assert response.status_code == 400 |
| 59 | assert response.json() == {"detail": "Cannot parse request body"} |
| 60 | |
| 61 | |
| 62 | @pytest.mark.parametrize( |
| 63 | "route,status_code,json", |
| 64 | [ |
| 65 | ("/error/404", 404, {"detail": "Not Found"}), |
| 66 | ("/error/custom", 422, {"custom": True}), |
| 67 | ], |
| 68 | ) |
| 69 | def test_exceptions(route, status_code, json): |
| 70 | response = client.post(route) |
| 71 | assert response.status_code == status_code |
| 72 | assert response.json() == json |
| 73 | |
| 74 | |
| 75 | @pytest.mark.asyncio |
| 76 | async def test_asyncio_exceptions(): |
| 77 | api = NinjaAPI() |
| 78 | |
| 79 | @api.get("/error") |
| 80 | async def thrower(request): |
| 81 | raise Http404("test") |
| 82 | |
| 83 | client = TestAsyncClient(api) |
| 84 | response = await client.get("/error") |
| 85 | assert response.status_code == 404 |
| 86 | |
| 87 | |
| 88 | def test_no_handlers(): |
| 89 | api = NinjaAPI() |
| 90 | api._exception_handlers = {} |
| 91 | |
| 92 | @api.get("/error") |
| 93 | def thrower(request): |
| 94 | raise RuntimeError("test") |
| 95 | |
| 96 | client = TestClient(api) |
| 97 | |
| 98 | with pytest.raises(RuntimeError): |
| 99 | client.get("/error") |