u-toolkit 0.1.7__py3-none-any.whl → 0.1.9__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 -13
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.9.dist-info}/METADATA +1 -1
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.9.dist-info}/RECORD +5 -5
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.9.dist-info}/WHEEL +0 -0
- {u_toolkit-0.1.7.dist-info → u_toolkit-0.1.9.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, Protocol, cast
|
3
|
+
from typing import Any, Generic, Literal, Protocol, 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
|
@@ -39,13 +43,17 @@ class HTTPErrorInterface(Protocol):
|
|
39
43
|
def response_class(cls) -> type[BaseModel]: ...
|
40
44
|
|
41
45
|
|
42
|
-
class NamedHTTPError(Exception, Generic[BaseModelT]):
|
46
|
+
class NamedHTTPError(Exception, Generic[WrapperErrorT, BaseModelT]):
|
43
47
|
status: int = status.HTTP_400_BAD_REQUEST
|
44
48
|
code: str | None = None
|
45
|
-
targets: Sequence[
|
46
|
-
target_transform: Callable[[
|
49
|
+
targets: Sequence[Any] | None = None
|
50
|
+
target_transform: Callable[[Any], Any] | None = None
|
47
51
|
message: str | None = None
|
48
|
-
|
52
|
+
wrapper: (
|
53
|
+
type[WrapperError[BaseModelT]]
|
54
|
+
| tuple[WrapperErrorT, Callable[[BaseModelT], WrapperErrorT]]
|
55
|
+
| None
|
56
|
+
) = None
|
49
57
|
|
50
58
|
@classmethod
|
51
59
|
def error_name(cls):
|
@@ -87,7 +95,7 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
87
95
|
target: str | None = None,
|
88
96
|
headers: dict[str, str] | None = None,
|
89
97
|
) -> None:
|
90
|
-
kwargs = {
|
98
|
+
kwargs: dict[str, Any] = {
|
91
99
|
"code": self.error_code(),
|
92
100
|
"message": message or "operation failed",
|
93
101
|
}
|
@@ -99,10 +107,13 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
99
107
|
kwargs["message"] = kwargs["message"].format(target=target)
|
100
108
|
|
101
109
|
self.model = self.model_class()(**kwargs)
|
110
|
+
create: Callable[[BaseModelT], BaseModel] | None = None
|
111
|
+
if inspect.isclass(self.wrapper):
|
112
|
+
create = self.wrapper.create
|
113
|
+
elif isinstance(self.wrapper, tuple):
|
114
|
+
create = self.wrapper[1]
|
102
115
|
self.data: BaseModel = (
|
103
|
-
self.
|
104
|
-
if self.wrapper_class is not None
|
105
|
-
else self.model
|
116
|
+
create(self.model) if create is not None else self.model
|
106
117
|
)
|
107
118
|
|
108
119
|
self.headers = headers
|
@@ -116,7 +127,16 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
|
|
116
127
|
@classmethod
|
117
128
|
def response_class(cls):
|
118
129
|
model = cls.model_class()
|
119
|
-
|
130
|
+
|
131
|
+
if cls.wrapper:
|
132
|
+
wrapper: Any
|
133
|
+
if inspect.isclass(cls.wrapper):
|
134
|
+
wrapper = cls.wrapper
|
135
|
+
else:
|
136
|
+
wrapper = cls.wrapper[0]
|
137
|
+
return wrapper[model]
|
138
|
+
|
139
|
+
return model
|
120
140
|
|
121
141
|
@classmethod
|
122
142
|
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=P9apEEQvbBMmffiHX57768gmemMeAy69-nb-1JQZAZE,4624
|
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.9.dist-info/METADATA,sha256=5Cqv8wacCiIyr68zBiVWhghEUxDQTOYP848G52cGaq0,365
|
34
|
+
u_toolkit-0.1.9.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
35
|
+
u_toolkit-0.1.9.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
|
36
|
+
u_toolkit-0.1.9.dist-info/RECORD,,
|
File without changes
|
File without changes
|