Coverage for src/data_api/api/errors.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 19:28 +0000

1"""Safe internal errors inside the normal HTTP middleware stack.""" 

2 

3import logging 

4from collections.abc import Iterator 

5from contextlib import contextmanager 

6 

7from fastapi import HTTPException 

8 

9from data_api.core.exceptions import ( 

10 INTERNAL_ERROR_DETAIL, 

11 DataNotAvailableException, 

12 UpstreamException, 

13) 

14 

15logger = logging.getLogger(__name__) 

16 

17 

18@contextmanager 

19def service_errors() -> Iterator[None]: 

20 try: 

21 yield 

22 except DataNotAvailableException, UpstreamException, HTTPException: 

23 raise 

24 except Exception as exc: 

25 logger.error("Unexpected request failure", exc_info=exc) 

26 raise HTTPException(status_code=500, detail=INTERNAL_ERROR_DETAIL) from exc