af-fastapi-exceptions 0.0.2__tar.gz → 0.0.4__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.2
3
+ Version: 0.0.4
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.2"
3
+ version = "0.0.4"
4
4
  description = "Reusable FastAPI base exceptions, ApiMessage, and a unified exception handler"
5
5
  authors = ["Allfly <engineering@allfly.io>"]
6
6
  license = "MIT"
@@ -45,6 +45,9 @@ class EntityNotFoundError(ServiceError):
45
45
  status_code = status.HTTP_404_NOT_FOUND
46
46
  super().__init__(message, status_code)
47
47
 
48
+ class EntityNotFoundWarning(ServiceWarning):
49
+ def __init__(self, message="Entity not found"):
50
+ super().__init__(message, status.HTTP_404_NOT_FOUND)
48
51
 
49
52
  class ResourceConflictError(ServiceError):
50
53
  def __init__(self, message="Request creates a conflict"):
@@ -68,3 +71,4 @@ class DownstreamServiceError(ServiceError):
68
71
  def __init__(self, message="Downstream service error"):
69
72
  status_code = status.HTTP_502_BAD_GATEWAY
70
73
  super().__init__(message, status_code)
74
+
@@ -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