spakky-fastapi 1.6.2__py3-none-any.whl → 1.6.3__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.
spakky_fastapi/error.py CHANGED
@@ -1,45 +1,65 @@
1
- import traceback
1
+ from typing import ClassVar
2
2
 
3
3
  from fastapi import status
4
+ from fastapi.responses import ORJSONResponse
4
5
  from spakky.core.error import SpakkyCoreError
5
6
 
6
7
 
7
8
  class SpakkyFastAPIError(SpakkyCoreError):
8
- status_code: int
9
+ status_code: ClassVar[int]
9
10
 
10
11
  def __init__(self, error: SpakkyCoreError) -> None:
11
12
  self.message = error.message
12
13
  self.args = error.args
13
14
 
15
+ def to_response(self) -> ORJSONResponse:
16
+ return ORJSONResponse(
17
+ content={
18
+ "message": self.message,
19
+ "args": [str(x) for x in self.args],
20
+ },
21
+ status_code=self.status_code,
22
+ )
23
+
14
24
 
15
25
  class SpakkyUnknownError(SpakkyCoreError):
16
26
  message = "알 수 없는 오류가 발생했습니다."
17
27
 
18
28
 
19
29
  class BadRequest(SpakkyFastAPIError):
20
- status_code: int = status.HTTP_400_BAD_REQUEST
30
+ status_code: ClassVar[int] = status.HTTP_400_BAD_REQUEST
21
31
 
22
32
 
23
33
  class Unauthorized(SpakkyFastAPIError):
24
- status_code: int = status.HTTP_401_UNAUTHORIZED
34
+ status_code: ClassVar[int] = status.HTTP_401_UNAUTHORIZED
25
35
 
26
36
 
27
37
  class Forbidden(SpakkyFastAPIError):
28
- status_code: int = status.HTTP_403_FORBIDDEN
38
+ status_code: ClassVar[int] = status.HTTP_403_FORBIDDEN
29
39
 
30
40
 
31
41
  class NotFound(SpakkyFastAPIError):
32
- status_code: int = status.HTTP_404_NOT_FOUND
42
+ status_code: ClassVar[int] = status.HTTP_404_NOT_FOUND
33
43
 
34
44
 
35
45
  class Conflict(SpakkyFastAPIError):
36
- status_code: int = status.HTTP_409_CONFLICT
46
+ status_code: ClassVar[int] = status.HTTP_409_CONFLICT
37
47
 
38
48
 
39
49
  class InternalServerError(SpakkyFastAPIError):
40
- status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
41
- traceback: str = ""
50
+ status_code: ClassVar[int] = status.HTTP_500_INTERNAL_SERVER_ERROR
51
+ stacktrace: str | None
42
52
 
43
- def __init__(self, error: Exception) -> None:
53
+ def __init__(self, error: Exception, stacktrace: str | None) -> None:
44
54
  super().__init__(SpakkyUnknownError(error.args))
45
- self.traceback = traceback.format_exc()
55
+ self.stacktrace = stacktrace
56
+
57
+ def to_response(self) -> ORJSONResponse:
58
+ return ORJSONResponse(
59
+ content={
60
+ "message": self.message,
61
+ "args": [str(x) for x in self.args],
62
+ "stacktrace": self.stacktrace,
63
+ },
64
+ status_code=self.status_code,
65
+ )
@@ -2,8 +2,6 @@ import traceback
2
2
  from typing import Callable, Awaitable, TypeAlias
3
3
 
4
4
  from fastapi import Request
5
- from fastapi.responses import ORJSONResponse
6
- from pydantic import BaseModel
7
5
  from starlette.middleware.base import BaseHTTPMiddleware, DispatchFunction
8
6
  from starlette.responses import Response
9
7
  from starlette.types import ASGIApp
@@ -13,12 +11,6 @@ from spakky_fastapi.error import InternalServerError, SpakkyFastAPIError
13
11
  Next: TypeAlias = Callable[[Request], Awaitable[Response]]
14
12
 
15
13
 
16
- class ErrorResponse(BaseModel):
17
- message: str
18
- args: list[str]
19
- traceback: str = ""
20
-
21
-
22
14
  class ErrorHandlingMiddleware(BaseHTTPMiddleware):
23
15
  __debug: bool
24
16
 
@@ -35,23 +27,11 @@ class ErrorHandlingMiddleware(BaseHTTPMiddleware):
35
27
  try:
36
28
  return await call_next(request)
37
29
  except SpakkyFastAPIError as e:
38
- return ORJSONResponse(
39
- content=ErrorResponse(
40
- message=e.message,
41
- args=[str(x) for x in e.args],
42
- ).model_dump(),
43
- status_code=e.status_code,
44
- )
45
- # pylint: disable=broad-exception-caught
46
- except Exception as e:
30
+ return e.to_response()
31
+ except Exception as e: # pylint: disable=broad-exception-caught
47
32
  if self.__debug:
48
33
  traceback.print_exc() # pragma: no cover
49
- error = InternalServerError(e)
50
- return ORJSONResponse(
51
- content=ErrorResponse(
52
- message=error.message,
53
- args=[str(x) for x in error.args],
54
- traceback=error.traceback if self.__debug else "",
55
- ).model_dump(),
56
- status_code=error.status_code,
57
- )
34
+ return InternalServerError(
35
+ error=e,
36
+ stacktrace=traceback.format_exc() if self.__debug else None,
37
+ ).to_response()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: spakky-fastapi
3
- Version: 1.6.2
3
+ Version: 1.6.3
4
4
  Summary: Highly abstracted Framework core to use DDD & DI/IoC & AOP & Etc...
5
5
  Author: Spakky
6
6
  Author-email: sejong418@icloud.com
@@ -1,9 +1,9 @@
1
1
  spakky_fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  spakky_fastapi/aspects/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  spakky_fastapi/aspects/authenticate.py,sha256=Nzyh7iVJy5OmQ3x2ZCPobhPNWgvK604IHaN0pmTdjkI,3997
4
- spakky_fastapi/error.py,sha256=SWyNZk1kxbTCiy-tLxB8saLpX5rBxwvy7q7JRCtNQV0,1124
4
+ spakky_fastapi/error.py,sha256=gLZObCZQpBjFmUstRWNDWy2if5DvNIY3WwY6jruwhak,1833
5
5
  spakky_fastapi/middlewares/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- spakky_fastapi/middlewares/error_handling.py,sha256=09cLArDGsN4mMIcC3qWr3SGivLfA6rUQ0PaRwQIT-YA,1816
6
+ spakky_fastapi/middlewares/error_handling.py,sha256=_ThkgNkDjVzegxz-QDpMxp1-grG1gx5vrhF4o3Lb2hE,1211
7
7
  spakky_fastapi/plugins/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  spakky_fastapi/plugins/authenticate.py,sha256=63tsHFjEzHklsmNyjPoksqiwiwo04h1dJjpzJq9pRf0,434
9
9
  spakky_fastapi/plugins/fast_api.py,sha256=K09znsenVDu7t-NNPSRtNxX7nzutDxBJqUGy20yT3oM,645
@@ -11,6 +11,6 @@ spakky_fastapi/post_processor.py,sha256=viHvWb_IWB1nnD4SHnCEwFgg8Wc25FV0q81UO5Fq
11
11
  spakky_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
12
  spakky_fastapi/stereotypes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  spakky_fastapi/stereotypes/api_controller.py,sha256=NM0gyTQfuHnENtvyfm5T9Y427gQdxdUcVYDzAXryF2M,19320
14
- spakky_fastapi-1.6.2.dist-info/METADATA,sha256=G_nhQGtaCzoatHomJYYyly4YmbIw_tRFoDnBrT-soUs,1853
15
- spakky_fastapi-1.6.2.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
16
- spakky_fastapi-1.6.2.dist-info/RECORD,,
14
+ spakky_fastapi-1.6.3.dist-info/METADATA,sha256=6FGe20gqns7mLrrbqEFI2lCprpFX2hIJZXlyUSSjCpA,1853
15
+ spakky_fastapi-1.6.3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
16
+ spakky_fastapi-1.6.3.dist-info/RECORD,,