u-toolkit 0.1.5__py3-none-any.whl → 0.1.7__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.
@@ -1,12 +1,20 @@
1
1
  from collections.abc import Callable, Sequence
2
2
  from typing import Generic, Literal, Protocol, cast
3
3
 
4
- from fastapi import status
4
+ from fastapi import Response, status
5
+ from fastapi.responses import JSONResponse, ORJSONResponse
6
+ from fastapi.utils import is_body_allowed_for_status_code
5
7
  from pydantic import BaseModel, create_model
6
8
 
7
9
  from u_toolkit.pydantic.type_vars import BaseModelT
8
10
 
9
11
 
12
+ try:
13
+ import orjson
14
+ except ImportError: # pragma: nocover
15
+ orjson = None # type: ignore
16
+
17
+
10
18
  class WrapperError(BaseModel, Generic[BaseModelT]): # type: ignore
11
19
  @classmethod
12
20
  def create(
@@ -75,13 +83,13 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
75
83
  def __init__(
76
84
  self,
77
85
  *,
78
- message: str,
86
+ message: str | None = None,
79
87
  target: str | None = None,
80
88
  headers: dict[str, str] | None = None,
81
89
  ) -> None:
82
90
  kwargs = {
83
91
  "code": self.error_code(),
84
- "message": message,
92
+ "message": message or "operation failed",
85
93
  }
86
94
 
87
95
  if target:
@@ -113,3 +121,23 @@ class NamedHTTPError(Exception, Generic[BaseModelT]):
113
121
  @classmethod
114
122
  def response_schema(cls):
115
123
  return {cls.status: {"model": cls.response_class()}}
124
+
125
+
126
+ def named_http_error_handler(_, exc: NamedHTTPError):
127
+ headers = exc.headers
128
+
129
+ if not is_body_allowed_for_status_code(exc.status):
130
+ return Response(status_code=exc.status, headers=headers)
131
+
132
+ if orjson:
133
+ return ORJSONResponse(
134
+ exc.data.model_dump(exclude_none=True),
135
+ status_code=exc.status,
136
+ headers=headers,
137
+ )
138
+
139
+ return JSONResponse(
140
+ exc.data.model_dump(exclude_none=True),
141
+ status_code=exc.status,
142
+ headers=headers,
143
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: u-toolkit
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: pydantic>=2.11.3
@@ -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=5E4wAJYwp0RJ4SEBVkchOgrgfwCgniQ8Mtg1O5sWUXE,3288
16
+ u_toolkit/fastapi/exception.py,sha256=ON2yx7sIIlwvlk6VtBWL4iXOioL6qCZC7rlNFn_et0g,4064
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.5.dist-info/METADATA,sha256=4JnI6t-zFqp7sGsXFTxSX_JvpnwspQVEmcXJ1PbHe_g,365
34
- u_toolkit-0.1.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
- u_toolkit-0.1.5.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
36
- u_toolkit-0.1.5.dist-info/RECORD,,
33
+ u_toolkit-0.1.7.dist-info/METADATA,sha256=kfRXER25SA83kSQZagcTTNWSSm40zu5xA6w83cMBIeQ,365
34
+ u_toolkit-0.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
+ u_toolkit-0.1.7.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
36
+ u_toolkit-0.1.7.dist-info/RECORD,,