1111import pytest
1212from asyncpg import PostgresError
1313from aws_library .s3 ._errors import S3AccessError , S3KeyNotFoundError
14- from fastapi import FastAPI , status
14+ from fastapi import FastAPI , HTTPException , status
15+ from fastapi .exceptions import RequestValidationError
1516from httpx import AsyncClient
1617from simcore_service_storage .api .rest .utils import set_exception_handlers
1718from simcore_service_storage .exceptions .errors import (
2122 ProjectAccessRightError ,
2223 ProjectNotFoundError ,
2324)
25+ from simcore_service_storage .modules .datcore_adapter .datcore_adapter_exceptions import (
26+ DatcoreAdapterTimeoutError ,
27+ )
2428from simcore_service_storage .modules .db .db_access_layer import (
2529 InvalidFileIdentifierError ,
2630)
@@ -88,6 +92,14 @@ async def client(initialized_app: FastAPI) -> AsyncIterator[AsyncClient]:
8892 S3AccessError (),
8993 status .HTTP_503_SERVICE_UNAVAILABLE ,
9094 ),
95+ (
96+ DatcoreAdapterTimeoutError (msg = "pytest datcore adapter timeout" ),
97+ status .HTTP_504_GATEWAY_TIMEOUT ,
98+ ),
99+ (
100+ NotImplementedError ("pytest not implemented error" ),
101+ status .HTTP_501_NOT_IMPLEMENTED ,
102+ ),
91103 ],
92104 ids = str ,
93105)
@@ -104,3 +116,41 @@ async def test_endpoint():
104116 response = await client .get ("/test" )
105117 assert response .status_code == status_code
106118 assert response .json () == {"errors" : [f"{ exception } " ]}
119+
120+
121+ async def test_generic_http_exception_handler (
122+ initialized_app : FastAPI , client : AsyncClient
123+ ):
124+ @initialized_app .get ("/test" )
125+ async def test_endpoint ():
126+ raise HTTPException (status_code = status .HTTP_410_GONE )
127+
128+ response = await client .get ("/test" )
129+ assert response .status_code == status .HTTP_410_GONE
130+ assert response .json () == {"errors" : ["Gone" ]}
131+
132+
133+ async def test_request_validation_error_handler (
134+ initialized_app : FastAPI , client : AsyncClient
135+ ):
136+ @initialized_app .get ("/test" )
137+ async def test_endpoint ():
138+ raise RequestValidationError (errors = ["pytest request validation error" ])
139+
140+ response = await client .get ("/test" )
141+ assert response .status_code == status .HTTP_422_UNPROCESSABLE_ENTITY
142+ assert response .json () == {"errors" : ["pytest request validation error" ]}
143+
144+
145+ @pytest .mark .xfail (
146+ reason = "Generic exception handler is not working as expected as shown in https://github.com/ITISFoundation/osparc-simcore/blob/5732a12e07e63d5ce55010ede9b9ab543bb9b278/packages/service-library/tests/fastapi/test_exceptions_utils.py"
147+ )
148+ async def test_generic_exception_handler (initialized_app : FastAPI , client : AsyncClient ):
149+ @initialized_app .get ("/test" )
150+ async def test_endpoint ():
151+ msg = "Generic exception"
152+ raise Exception (msg )
153+
154+ response = await client .get ("/test" )
155+ assert response .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
156+ assert response .json () == {"detail" : "Generic exception" }
0 commit comments