u-toolkit 0.1.7__py3-none-any.whl → 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- u_toolkit/fastapi/exception.py +33 -20
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.8.dist-info}/METADATA +1 -1
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.8.dist-info}/RECORD +5 -5
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.8.dist-info}/WHEEL +0 -0
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.8.dist-info}/entry_points.txt +0 -0
u_toolkit/fastapi/exception.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
import inspect
|
1
2
|
from collections.abc import Callable, Sequence
|
2
|
-
from typing import Generic, Literal,
|
3
|
+
from typing import Any, Generic, Literal, TypeVar, cast
|
3
4
|
|
4
5
|
from fastapi import Response, status
|
5
6
|
from fastapi.responses import JSONResponse, ORJSONResponse
|
@@ -10,12 +11,12 @@ from u_toolkit.pydantic.type_vars import BaseModelT
|
|
10
11
|
|
11
12
|
|
12
13
|
try:
|
13
|
-
import orjson
|
14
|
+
import orjson # type: ignore
|
14
15
|
except ImportError: # pragma: nocover
|
15
16
|
orjson = None # type: ignore
|
16
17
|
|
17
18
|
|
18
|
-
class WrapperError(BaseModel, Generic[BaseModelT]):
|
19
|
+
class WrapperError(BaseModel, Generic[BaseModelT]):
|
19
20
|
@classmethod
|
20
21
|
def create(
|
21
22
|
cls: type["WrapperError[BaseModelT]"],
|
@@ -24,7 +25,10 @@ class WrapperError(BaseModel, Generic[BaseModelT]): # type: ignore
|
|
24
25
|
raise NotImplementedError
|
25
26
|
|
26
27
|
|
27
|
-
|
28
|
+
WrapperErrorT = TypeVar("WrapperErrorT", bound=WrapperError)
|
29
|
+
|
30
|
+
|
31
|
+
class EndpointError(WrapperError[BaseModelT], Generic[BaseModelT]):
|
28
32
|
error: BaseModelT
|
29
33
|
|
30
34
|
@classmethod
|
@@ -32,20 +36,17 @@ class EndpointError(WrapperError, Generic[BaseModelT]):
|
|
32
36
|
return cls(error=model)
|
33
37
|
|
34
38
|
|
35
|
-
class
|
36
|
-
status: int
|
37
|
-
|
38
|
-
@classmethod
|
39
|
-
def response_class(cls) -> type[BaseModel]: ...
|
40
|
-
|
41
|
-
|
42
|
-
class NamedHTTPError(Exception, Generic[BaseModelT]):
|
39
|
+
class NamedHTTPError(Exception, Generic[WrapperErrorT, BaseModelT]):
|
43
40
|
status: int = status.HTTP_400_BAD_REQUEST
|
44
41
|
code: str | None = None
|
45
|
-
targets: Sequence[
|
46
|
-
target_transform: Callable[[
|
42
|
+
targets: Sequence[Any] | None = None
|
43
|
+
target_transform: Callable[[Any], Any] | None = None
|
47
44
|
message: str | None = None
|
48
|
-
|
45
|
+
wrapper: (
|
46
|
+
type[WrapperError[BaseModelT]]
|
47
|
+
| tuple[WrapperErrorT, Callable[[BaseModelT], WrapperErrorT]]
|
48
|
+
| None
|
49
|
+
) = None
|
49
50
|
|
50
51
|
@classmethod
|
51
52
|
def error_name(cls):
|
@@ -87,7 +88,7 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
87
88
|
target: str | None = None,
|
88
89
|
headers: dict[str, str] | None = None,
|
89
90
|
) -> None:
|
90
|
-
kwargs = {
|
91
|
+
kwargs: dict[str, Any] = {
|
91
92
|
"code": self.error_code(),
|
92
93
|
"message": message or "operation failed",
|
93
94
|
}
|
@@ -99,10 +100,13 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
99
100
|
kwargs["message"] = kwargs["message"].format(target=target)
|
100
101
|
|
101
102
|
self.model = self.model_class()(**kwargs)
|
103
|
+
create: Callable[[BaseModelT], BaseModel] | None = None
|
104
|
+
if inspect.isclass(self.wrapper):
|
105
|
+
create = self.wrapper.create
|
106
|
+
elif isinstance(self.wrapper, tuple):
|
107
|
+
create = self.wrapper[1]
|
102
108
|
self.data: BaseModel = (
|
103
|
-
self.
|
104
|
-
if self.wrapper_class is not None
|
105
|
-
else self.model
|
109
|
+
create(self.model) if create is not None else self.model
|
106
110
|
)
|
107
111
|
|
108
112
|
self.headers = headers
|
@@ -116,7 +120,16 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
116
120
|
@classmethod
|
117
121
|
def response_class(cls):
|
118
122
|
model = cls.model_class()
|
119
|
-
|
123
|
+
|
124
|
+
if cls.wrapper:
|
125
|
+
wrapper: Any
|
126
|
+
if inspect.isclass(cls.wrapper):
|
127
|
+
wrapper = cls.wrapper
|
128
|
+
else:
|
129
|
+
wrapper = cls.wrapper[0]
|
130
|
+
return wrapper[model]
|
131
|
+
|
132
|
+
return model
|
120
133
|
|
121
134
|
@classmethod
|
122
135
|
def response_schema(cls):
|
@@ -13,7 +13,7 @@ u_toolkit/signature.py,sha256=-Q6n28PYBYYdd2OXBKESeVkL2rYpV6EaY3IVwQmzezQ,2161
|
|
13
13
|
u_toolkit/fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
14
14
|
u_toolkit/fastapi/cbv.py,sha256=gtNhV8mQAEQNf9uIwyjlrA-ZbVwLeJenJ-f2G2Co1SE,10808
|
15
15
|
u_toolkit/fastapi/config.py,sha256=kGpokR9XXr1KxMA1GVKYkCdKwqIQAIwOJ-v6sGbqzAQ,267
|
16
|
-
u_toolkit/fastapi/exception.py,sha256=
|
16
|
+
u_toolkit/fastapi/exception.py,sha256=fttKqG2yjkG_sBin9_neZ7dLTmTs-QVAsrEbdiSifog,4490
|
17
17
|
u_toolkit/fastapi/helpers.py,sha256=BCMMLxa1c6BMA_rKq-hCi0iyEjrR3Z5rPMeTvgaVJB0,447
|
18
18
|
u_toolkit/fastapi/lifespan.py,sha256=W1TwWymW7xtmntx59QBC4LQ6xQr3L7yuMMGj4U8hhTQ,1813
|
19
19
|
u_toolkit/fastapi/pagination.py,sha256=yOgEDUT04m_mZ0cPliuDbUHLFnmxGAmr5PyZlwfjT_s,1940
|
@@ -30,7 +30,7 @@ u_toolkit/sqlalchemy/type_vars.py,sha256=m2VeV41CBIK_1QX3w2kgz-n556sILAGZ-Kaz3TD
|
|
30
30
|
u_toolkit/sqlalchemy/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
31
31
|
u_toolkit/sqlalchemy/orm/fields.py,sha256=3zoYil23I6YLtc_59aHDt9w5l1NBTkePT9AfXI3DMiY,593
|
32
32
|
u_toolkit/sqlalchemy/orm/models.py,sha256=V8vf4ps3phAmwxyaFYK7pw8Igz7h097o4QBjKB0gwC8,705
|
33
|
-
u_toolkit-0.1.
|
34
|
-
u_toolkit-0.1.
|
35
|
-
u_toolkit-0.1.
|
36
|
-
u_toolkit-0.1.
|
33
|
+
u_toolkit-0.1.8.dist-info/METADATA,sha256=VNzIKrOc46ToPyEaNPPcyg5rII9h53DTI-VmilMwExw,365
|
34
|
+
u_toolkit-0.1.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
35
|
+
u_toolkit-0.1.8.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
|
36
|
+
u_toolkit-0.1.8.dist-info/RECORD,,
|
File without changes
|
File without changes
|