"""Error handler tests.""" from fastapi import HTTPException, status from fastapi.testclient import TestClient from sdlc_training_charliet.api.error_handlers import DEFAULT_ERROR_MSG def exc_route(): """Fake handler that only raises an exception.""" raise Exception("something internal to pdp happened!") def http_exc_route(): """Fake handler that only raises an HTTPException.""" raise HTTPException(detail="error message for the client.", status_code=400) def test_default_error_handler(app) -> None: """Verify an uncaught exception returns a 500 with expected payload.""" with TestClient(app, raise_server_exceptions=False) as test_client: app.add_api_route("/test_default_error_handler", endpoint=exc_route) response = test_client.get("/test_default_error_handler") assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR err = response.json() assert err["code"] == "internal_error" assert err["message"] == DEFAULT_ERROR_MSG def test_http_error_handler(app) -> None: """Verify an uncaught exception returns a 4xx with expected payload.""" with TestClient(app, raise_server_exceptions=False) as test_client: app.add_api_route("/test_http_error_handler", endpoint=http_exc_route) response = test_client.get("/test_http_error_handler") assert response.status_code == status.HTTP_400_BAD_REQUEST err = response.json() assert err["code"] == "bad_request" assert err["message"] == "error message for the client."