diff --git a/stapi-fastapi/CHANGELOG.md b/stapi-fastapi/CHANGELOG.md index 0c7517c..8f8c8d7 100644 --- a/stapi-fastapi/CHANGELOG.md +++ b/stapi-fastapi/CHANGELOG.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## Changed +- Renamed all exceptions to errors ([#41](https://github.com/stapi-spec/pystapi/pull/41)) - stapi-fastapi is now using stapi-pydantic models, deduplicating code - Product in stapi-fastapi is now subclass of Product from stapi-pydantic diff --git a/stapi-fastapi/src/stapi_fastapi/backends/product_backend.py b/stapi-fastapi/src/stapi_fastapi/backends/product_backend.py index b554f79..aa74510 100644 --- a/stapi-fastapi/src/stapi_fastapi/backends/product_backend.py +++ b/stapi-fastapi/src/stapi_fastapi/backends/product_backend.py @@ -43,7 +43,7 @@ Note: Backends must validate search queryables and return - returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid. + returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid. """ SearchOpportunitiesAsync = Callable[ @@ -63,8 +63,8 @@ - Should return returns.result.Success[OpportunitySearchRecord] - Returning returns.result.Failure[Exception] will result in a 500. -Backends must validate search queryables and return -returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid. + Backends must validate search queryables and return +returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid. """ GetOpportunityCollection = Callable[ @@ -105,5 +105,5 @@ Note: Backends must validate order payload and return - returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid. + returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid. """ diff --git a/stapi-fastapi/src/stapi_fastapi/exceptions.py b/stapi-fastapi/src/stapi_fastapi/errors.py similarity index 72% rename from stapi-fastapi/src/stapi_fastapi/exceptions.py rename to stapi-fastapi/src/stapi_fastapi/errors.py index bee04f5..1ae870c 100644 --- a/stapi-fastapi/src/stapi_fastapi/exceptions.py +++ b/stapi-fastapi/src/stapi_fastapi/errors.py @@ -3,15 +3,15 @@ from fastapi import HTTPException, status -class StapiException(HTTPException): +class StapiError(HTTPException): pass -class QueryablesException(StapiException): +class QueryablesError(StapiError): def __init__(self, detail: Any) -> None: super().__init__(status.HTTP_422_UNPROCESSABLE_ENTITY, detail) -class NotFoundException(StapiException): +class NotFoundError(StapiError): def __init__(self, detail: Any | None = None) -> None: super().__init__(status.HTTP_404_NOT_FOUND, detail) diff --git a/stapi-fastapi/src/stapi_fastapi/routers/product_router.py b/stapi-fastapi/src/stapi_fastapi/routers/product_router.py index 52e6d6b..cbc1168 100644 --- a/stapi-fastapi/src/stapi_fastapi/routers/product_router.py +++ b/stapi-fastapi/src/stapi_fastapi/routers/product_router.py @@ -34,7 +34,7 @@ ) from stapi_fastapi.constants import TYPE_JSON -from stapi_fastapi.exceptions import NotFoundException, QueryablesException +from stapi_fastapi.errors import NotFoundError, QueryablesError from stapi_fastapi.models.product import Product from stapi_fastapi.responses import GeoJSONResponse from stapi_fastapi.routers.route_names import ( @@ -300,7 +300,7 @@ async def search_opportunities_sync( links.append(self.pagination_link(request, search, x)) case Maybe.empty: pass - case Failure(e) if isinstance(e, QueryablesException): + case Failure(e) if isinstance(e, QueryablesError): raise e case Failure(e): logger.error( @@ -339,7 +339,7 @@ async def search_opportunities_async( content=search_record.model_dump(mode="json"), headers=headers, ) - case Failure(e) if isinstance(e, QueryablesException): + case Failure(e) if isinstance(e, QueryablesError): raise e case Failure(e): logger.error( @@ -385,7 +385,7 @@ async def create_order(self, payload: OrderPayload, request: Request, response: location = str(self.root_router.generate_order_href(request, order.id)) response.headers["Location"] = location return order # type: ignore - case Failure(e) if isinstance(e, QueryablesException): + case Failure(e) if isinstance(e, QueryablesError): raise e case Failure(e): logger.error( @@ -449,7 +449,7 @@ async def get_opportunity_collection( ) return opportunity_collection # type: ignore case Success(Maybe.empty): - raise NotFoundException("Opportunity Collection not found") + raise NotFoundError("Opportunity Collection not found") case Failure(e): logger.error( "An error occurred while fetching opportunity collection: '%s': %s", diff --git a/stapi-fastapi/src/stapi_fastapi/routers/root_router.py b/stapi-fastapi/src/stapi_fastapi/routers/root_router.py index b7c71be..b6f0518 100644 --- a/stapi-fastapi/src/stapi_fastapi/routers/root_router.py +++ b/stapi-fastapi/src/stapi_fastapi/routers/root_router.py @@ -30,7 +30,7 @@ ) from stapi_fastapi.conformance import ASYNC_OPPORTUNITIES, CORE from stapi_fastapi.constants import TYPE_GEOJSON, TYPE_JSON -from stapi_fastapi.exceptions import NotFoundException +from stapi_fastapi.errors import NotFoundError from stapi_fastapi.models.product import Product from stapi_fastapi.responses import GeoJSONResponse from stapi_fastapi.routers.product_router import ProductRouter @@ -231,7 +231,7 @@ def get_products(self, request: Request, next: str | None = None, limit: int = 1 start = self.product_ids.index(next) except ValueError: logger.exception("An error occurred while retrieving products") - raise NotFoundException(detail="Error finding pagination token for products") from None + raise NotFoundError(detail="Error finding pagination token for products") from None end = start + limit ids = self.product_ids[start:end] links = [ @@ -262,7 +262,7 @@ async def get_orders( case Maybe.empty: pass case Failure(ValueError()): - raise NotFoundException(detail="Error finding pagination token") + raise NotFoundError(detail="Error finding pagination token") case Failure(e): logger.error( "An error occurred while retrieving orders: %s", @@ -285,7 +285,7 @@ async def get_order(self, order_id: str, request: Request) -> Order[OrderStatus] order.links.extend(self.order_links(order, request)) return order # type: ignore case Success(Maybe.empty): - raise NotFoundException("Order not found") + raise NotFoundError("Order not found") case Failure(e): logger.error( "An error occurred while retrieving order '%s': %s", @@ -316,9 +316,9 @@ async def get_order_statuses( case Maybe.empty: pass case Success(Maybe.empty): - raise NotFoundException("Order not found") + raise NotFoundError("Order not found") case Failure(ValueError()): - raise NotFoundException("Error finding pagination token") + raise NotFoundError("Error finding pagination token") case Failure(e): logger.error( "An error occurred while retrieving order statuses: %s", @@ -392,7 +392,7 @@ async def get_opportunity_search_records( case Maybe.empty: pass case Failure(ValueError()): - raise NotFoundException(detail="Error finding pagination token") + raise NotFoundError(detail="Error finding pagination token") case Failure(e): logger.error( "An error occurred while retrieving opportunity search records: %s", @@ -415,7 +415,7 @@ async def get_opportunity_search_record(self, search_record_id: str, request: Re search_record.links.append(self.opportunity_search_record_self_link(search_record, request)) return search_record # type: ignore case Success(Maybe.empty): - raise NotFoundException("Opportunity Search Record not found") + raise NotFoundError("Opportunity Search Record not found") case Failure(e): logger.error( "An error occurred while retrieving opportunity search record '%s': %s", @@ -439,7 +439,7 @@ async def get_opportunity_search_record_statuses( case Success(Some(search_record_statuses)): return search_record_statuses # type: ignore case Success(Maybe.empty): - raise NotFoundException("Opportunity Search Record not found") + raise NotFoundError("Opportunity Search Record not found") case Failure(e): logger.error( "An error occurred while retrieving opportunity search record statuses '%s': %s",