Skip to content
1 change: 1 addition & 0 deletions stapi-fastapi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions stapi-fastapi/src/stapi_fastapi/backends/product_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand All @@ -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[
Expand Down Expand Up @@ -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.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 5 additions & 5 deletions stapi-fastapi/src/stapi_fastapi/routers/product_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 9 additions & 9 deletions stapi-fastapi/src/stapi_fastapi/routers/root_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down