Skip to content

Commit 1f83a98

Browse files
refactor: rename errors to exceptions (#41)
## What I'm changing - Closes #28 ## How I did it - Simple refactor ## Checklist - [x] Tests pass: `uv run pytest` - [x] Checks pass: `uv run pre-commit --all-files` - [x] CHANGELOG is updated (if necessary) --------- Co-authored-by: Dylan <[email protected]>
1 parent 7f31f6b commit 1f83a98

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

stapi-fastapi/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1818

1919
## Changed
2020

21+
- Renamed all exceptions to errors ([#41](https://github.com/stapi-spec/pystapi/pull/41))
2122
- stapi-fastapi is now using stapi-pydantic models, deduplicating code
2223
- Product in stapi-fastapi is now subclass of Product from stapi-pydantic
2324

stapi-fastapi/src/stapi_fastapi/backends/product_backend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
4444
Note:
4545
Backends must validate search queryables and return
46-
returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid.
46+
returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid.
4747
"""
4848

4949
SearchOpportunitiesAsync = Callable[
@@ -63,8 +63,8 @@
6363
- Should return returns.result.Success[OpportunitySearchRecord]
6464
- Returning returns.result.Failure[Exception] will result in a 500.
6565
66-
Backends must validate search queryables and return
67-
returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid.
66+
Backends must validate search queryables and return
67+
returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid.
6868
"""
6969

7070
GetOpportunityCollection = Callable[
@@ -105,5 +105,5 @@
105105
106106
Note:
107107
Backends must validate order payload and return
108-
returns.result.Failure[stapi_fastapi.exceptions.QueryablesException] if not valid.
108+
returns.result.Failure[stapi_fastapi.errors.QueryablesError] if not valid.
109109
"""

stapi-fastapi/src/stapi_fastapi/exceptions.py renamed to stapi-fastapi/src/stapi_fastapi/errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from fastapi import HTTPException, status
44

55

6-
class StapiException(HTTPException):
6+
class StapiError(HTTPException):
77
pass
88

99

10-
class QueryablesException(StapiException):
10+
class QueryablesError(StapiError):
1111
def __init__(self, detail: Any) -> None:
1212
super().__init__(status.HTTP_422_UNPROCESSABLE_ENTITY, detail)
1313

1414

15-
class NotFoundException(StapiException):
15+
class NotFoundError(StapiError):
1616
def __init__(self, detail: Any | None = None) -> None:
1717
super().__init__(status.HTTP_404_NOT_FOUND, detail)

stapi-fastapi/src/stapi_fastapi/routers/product_router.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
)
3535

3636
from stapi_fastapi.constants import TYPE_JSON
37-
from stapi_fastapi.exceptions import NotFoundException, QueryablesException
37+
from stapi_fastapi.errors import NotFoundError, QueryablesError
3838
from stapi_fastapi.models.product import Product
3939
from stapi_fastapi.responses import GeoJSONResponse
4040
from stapi_fastapi.routers.route_names import (
@@ -300,7 +300,7 @@ async def search_opportunities_sync(
300300
links.append(self.pagination_link(request, search, x))
301301
case Maybe.empty:
302302
pass
303-
case Failure(e) if isinstance(e, QueryablesException):
303+
case Failure(e) if isinstance(e, QueryablesError):
304304
raise e
305305
case Failure(e):
306306
logger.error(
@@ -339,7 +339,7 @@ async def search_opportunities_async(
339339
content=search_record.model_dump(mode="json"),
340340
headers=headers,
341341
)
342-
case Failure(e) if isinstance(e, QueryablesException):
342+
case Failure(e) if isinstance(e, QueryablesError):
343343
raise e
344344
case Failure(e):
345345
logger.error(
@@ -385,7 +385,7 @@ async def create_order(self, payload: OrderPayload, request: Request, response:
385385
location = str(self.root_router.generate_order_href(request, order.id))
386386
response.headers["Location"] = location
387387
return order # type: ignore
388-
case Failure(e) if isinstance(e, QueryablesException):
388+
case Failure(e) if isinstance(e, QueryablesError):
389389
raise e
390390
case Failure(e):
391391
logger.error(
@@ -449,7 +449,7 @@ async def get_opportunity_collection(
449449
)
450450
return opportunity_collection # type: ignore
451451
case Success(Maybe.empty):
452-
raise NotFoundException("Opportunity Collection not found")
452+
raise NotFoundError("Opportunity Collection not found")
453453
case Failure(e):
454454
logger.error(
455455
"An error occurred while fetching opportunity collection: '%s': %s",

stapi-fastapi/src/stapi_fastapi/routers/root_router.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
)
3131
from stapi_fastapi.conformance import ASYNC_OPPORTUNITIES, CORE
3232
from stapi_fastapi.constants import TYPE_GEOJSON, TYPE_JSON
33-
from stapi_fastapi.exceptions import NotFoundException
33+
from stapi_fastapi.errors import NotFoundError
3434
from stapi_fastapi.models.product import Product
3535
from stapi_fastapi.responses import GeoJSONResponse
3636
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
231231
start = self.product_ids.index(next)
232232
except ValueError:
233233
logger.exception("An error occurred while retrieving products")
234-
raise NotFoundException(detail="Error finding pagination token for products") from None
234+
raise NotFoundError(detail="Error finding pagination token for products") from None
235235
end = start + limit
236236
ids = self.product_ids[start:end]
237237
links = [
@@ -262,7 +262,7 @@ async def get_orders(
262262
case Maybe.empty:
263263
pass
264264
case Failure(ValueError()):
265-
raise NotFoundException(detail="Error finding pagination token")
265+
raise NotFoundError(detail="Error finding pagination token")
266266
case Failure(e):
267267
logger.error(
268268
"An error occurred while retrieving orders: %s",
@@ -285,7 +285,7 @@ async def get_order(self, order_id: str, request: Request) -> Order[OrderStatus]
285285
order.links.extend(self.order_links(order, request))
286286
return order # type: ignore
287287
case Success(Maybe.empty):
288-
raise NotFoundException("Order not found")
288+
raise NotFoundError("Order not found")
289289
case Failure(e):
290290
logger.error(
291291
"An error occurred while retrieving order '%s': %s",
@@ -316,9 +316,9 @@ async def get_order_statuses(
316316
case Maybe.empty:
317317
pass
318318
case Success(Maybe.empty):
319-
raise NotFoundException("Order not found")
319+
raise NotFoundError("Order not found")
320320
case Failure(ValueError()):
321-
raise NotFoundException("Error finding pagination token")
321+
raise NotFoundError("Error finding pagination token")
322322
case Failure(e):
323323
logger.error(
324324
"An error occurred while retrieving order statuses: %s",
@@ -392,7 +392,7 @@ async def get_opportunity_search_records(
392392
case Maybe.empty:
393393
pass
394394
case Failure(ValueError()):
395-
raise NotFoundException(detail="Error finding pagination token")
395+
raise NotFoundError(detail="Error finding pagination token")
396396
case Failure(e):
397397
logger.error(
398398
"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
415415
search_record.links.append(self.opportunity_search_record_self_link(search_record, request))
416416
return search_record # type: ignore
417417
case Success(Maybe.empty):
418-
raise NotFoundException("Opportunity Search Record not found")
418+
raise NotFoundError("Opportunity Search Record not found")
419419
case Failure(e):
420420
logger.error(
421421
"An error occurred while retrieving opportunity search record '%s': %s",
@@ -439,7 +439,7 @@ async def get_opportunity_search_record_statuses(
439439
case Success(Some(search_record_statuses)):
440440
return search_record_statuses # type: ignore
441441
case Success(Maybe.empty):
442-
raise NotFoundException("Opportunity Search Record not found")
442+
raise NotFoundError("Opportunity Search Record not found")
443443
case Failure(e):
444444
logger.error(
445445
"An error occurred while retrieving opportunity search record statuses '%s': %s",

0 commit comments

Comments
 (0)