Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 375c51c

Browse files
parksjrtylandersonjkeifer
committed
fixes StapiExceptions
Co-authored-by: Tyler <[email protected]> Co-authored-by: Jarrett Keifer <[email protected]>
1 parent 1bde9f5 commit 375c51c

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

src/stapi_fastapi/backends/product_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def search_opportunities(
2020
Search for ordering opportunities for the given search parameters.
2121
2222
Backends must validate search constraints and raise
23-
`stapi_fastapi.backend.exceptions.ConstraintsException` if not valid.
23+
`stapi_fastapi.exceptions.ConstraintsException` if not valid.
2424
"""
2525
...
2626

@@ -34,6 +34,6 @@ async def create_order(
3434
Create a new order.
3535
3636
Backends must validate order payload and raise
37-
`stapi_fastapi.backend.exceptions.ConstraintsException` if not valid.
37+
`stapi_fastapi.exceptions.ConstraintsException` if not valid.
3838
"""
3939
...

src/stapi_fastapi/backends/root_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async def get_order(self, order_id: str, request: Request) -> Order:
1616
"""
1717
Get details for order with `order_id`.
1818
19-
Backends must raise `stapi_fastapi.backend.exceptions.NotFoundException`
19+
Backends must raise `stapi_fastapi.exceptions.NotFoundException`
2020
if not found or access denied.
2121
"""
2222
...

src/stapi_fastapi/exceptions.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
from typing import Any, Mapping
1+
from typing import Any
22

3-
from fastapi import HTTPException
3+
from fastapi import HTTPException, status
44

55

66
class StapiException(HTTPException):
7-
def __init__(self, status_code: int, detail: str) -> None:
8-
super().__init__(status_code, detail)
9-
7+
pass
108

11-
class ConstraintsException(Exception):
12-
detail: Mapping[str, Any] | None
139

14-
def __init__(self, detail: Mapping[str, Any] | None = None) -> None:
15-
super().__init__()
16-
self.detail = detail
10+
class ConstraintsException(StapiException):
11+
def __init__(self, detail: Any) -> None:
12+
super().__init__(status.HTTP_422_UNPROCESSABLE_ENTITY, detail)
1713

1814

19-
class NotFoundException(Exception):
20-
pass
15+
class NotFoundException(StapiException):
16+
def __init__(self, detail: Any) -> None:
17+
super().__init__(status.HTTP_404_NOT_FOUND, detail)

src/stapi_fastapi/models/shared.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional, Self, Union
1+
from typing import Any, Self
22

33
from pydantic import AnyUrl, BaseModel, ConfigDict
44

@@ -7,9 +7,9 @@ class Link(BaseModel):
77
href: AnyUrl
88
rel: str
99
type: str | None = None
10-
title: Optional[str] = None
11-
method: Optional[str] = None
12-
headers: Optional[dict[str, Union[str, list[str]]]] = None
10+
title: str | None = None
11+
method: str | None = None
12+
headers: dict[str, str | list[str]] | None = None
1313
body: Any = None
1414

1515
model_config = ConfigDict(extra="allow")

src/stapi_fastapi/routers/root_router.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from typing import Self
22

3-
from fastapi import APIRouter, HTTPException, Request, status
3+
from fastapi import APIRouter, Request
44
from fastapi.datastructures import URL
55

66
from stapi_fastapi.backends.root_backend import RootBackend
77
from stapi_fastapi.constants import TYPE_GEOJSON, TYPE_JSON
8-
from stapi_fastapi.exceptions import NotFoundException
98
from stapi_fastapi.models.order import Order
109
from stapi_fastapi.models.product import Product, ProductsCollection
1110
from stapi_fastapi.models.root import RootResponse
@@ -132,11 +131,7 @@ async def get_order(self: Self, order_id: str, request: Request) -> Order:
132131
"""
133132
Get details for order with `order_id`.
134133
"""
135-
try:
136-
order = await self.backend.get_order(order_id, request)
137-
except NotFoundException as exc:
138-
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="not found") from exc
139-
134+
order = await self.backend.get_order(order_id, request)
140135
order.links.append(Link(href=str(request.url), rel="self", type=TYPE_GEOJSON))
141136
return order
142137

0 commit comments

Comments
 (0)