af-fastapi-exceptions 0.0.3__tar.gz → 0.0.5__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: af-fastapi-exceptions
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: Reusable FastAPI base exceptions, ApiMessage, and a unified exception handler
5
5
  License: MIT
6
6
  Author: Allfly
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "af-fastapi-exceptions"
3
- version = "0.0.3"
3
+ version = "0.0.5"
4
4
  description = "Reusable FastAPI base exceptions, ApiMessage, and a unified exception handler"
5
5
  authors = ["Allfly <engineering@allfly.io>"]
6
6
  license = "MIT"
@@ -54,12 +54,21 @@ class ResourceConflictError(ServiceError):
54
54
  status_code = status.HTTP_409_CONFLICT
55
55
  super().__init__(message, status_code)
56
56
 
57
+ class ResourceConflictWarning(ServiceWarning):
58
+ def __init__(self, message="Request creates a conflict"):
59
+ status_code = status.HTTP_409_CONFLICT
60
+ super().__init__(message, status_code)
57
61
 
58
62
  class UnprocessableRequestError(ServiceError):
59
63
  def __init__(self, message="Unprocessable Request"):
60
64
  status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
61
65
  super().__init__(message, status_code)
62
66
 
67
+ class UnprocessableRequestWarning(ServiceWarning):
68
+ def __init__(self, message="Unprocessable Request"):
69
+ status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
70
+ super().__init__(message, status_code)
71
+
63
72
 
64
73
  class UnsupportedFeatureError(ServiceWarning):
65
74
  def __init__(self, message="Not implemented"):
@@ -3,7 +3,6 @@ from typing import Any, Protocol
3
3
 
4
4
  from fastapi.exceptions import RequestValidationError
5
5
  from loguru import logger
6
- from starlette import status
7
6
  from starlette.middleware.base import BaseHTTPMiddleware
8
7
  from starlette.requests import Request
9
8
  from starlette.responses import JSONResponse
@@ -35,7 +34,7 @@ def _handle_unexpected_error(
35
34
  return JSONResponse(
36
35
  content=body.model_dump(mode="json", exclude_none=True),
37
36
  media_type="application/json",
38
- status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
37
+ status_code=500,
39
38
  )
40
39
 
41
40
 
@@ -77,7 +76,7 @@ def build_validation_handler(error_model: type[ApiErrorBody]):
77
76
  logger.warning(detailed_error)
78
77
  body = error_model(message="Request Validation Error", errors=errors)
79
78
  return JSONResponse(
80
- status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
79
+ status_code=422,
81
80
  content=body.model_dump(mode="json", exclude_none=True),
82
81
  )
83
82
 
@@ -86,18 +85,18 @@ def build_validation_handler(error_model: type[ApiErrorBody]):
86
85
 
87
86
  def build_error_responses(error_model: type[ApiErrorBody]) -> dict:
88
87
  return {
89
- status.HTTP_401_UNAUTHORIZED: {
88
+ 401: {
90
89
  "model": error_model,
91
90
  "description": "Authentication data in the request is invalid",
92
91
  },
93
- status.HTTP_403_FORBIDDEN: {"model": error_model, "description": "Not authorized"},
94
- status.HTTP_404_NOT_FOUND: {"model": error_model, "description": "Not found"},
95
- status.HTTP_409_CONFLICT: {"model": error_model, "description": "Resource conflict detected"},
96
- status.HTTP_422_UNPROCESSABLE_ENTITY: {
92
+ 403: {"model": error_model, "description": "Not authorized"},
93
+ 404: {"model": error_model, "description": "Not found"},
94
+ 409: {"model": error_model, "description": "Resource conflict detected"},
95
+ 422: {
97
96
  "model": error_model,
98
97
  "description": "Understood the request, but can't and won't process it",
99
98
  },
100
- status.HTTP_400_BAD_REQUEST: {"model": error_model, "description": "Bad request"},
99
+ 400: {"model": error_model, "description": "Bad request"},
101
100
  }
102
101
 
103
102