Skip to content

Commit

Permalink
feat: http exception handler for formatting exception response, Profi…
Browse files Browse the repository at this point in the history
…ling initialized from env and project not found exception in project summary
  • Loading branch information
prabinoid committed Feb 24, 2025
1 parent 65de7ca commit 406e450
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config:
)
APP_NAME: str = "Tasking Manager"
DEBUG: bool = False
PROFILING: bool = os.getenv("PROFILING", False)
EXTRA_CORS_ORIGINS: list = []

# The base url the application is reachable
Expand Down
18 changes: 15 additions & 3 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,25 @@ async def custom_http_exception_handler(request: Request, exc: HTTPException):
status_code=exc.status_code,
headers={"WWW-Authenticate": "Bearer"},
)

if isinstance(exc.detail, dict) and "error" in exc.detail:
error_response = exc.detail
else:
error_response = {
"error": {
"code": exc.status_code,
"sub_code": "UNKNOWN_ERROR",
"message": str(exc.detail),
"details": {},
}
}

return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail},
content=error_response,
)

PROFILING = True # Set this from a settings model

PROFILING = settings.PROFILING
if PROFILING:

@_app.middleware("http")
Expand Down
3 changes: 2 additions & 1 deletion backend/services/project_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ async def get_project_summary(
params = {"id": project_id}
# Execute query
project = await db.fetch_one(query, params)

if not project:
raise NotFound(sub_code="PROJECT_NOT_FOUND", project_id=project_id)
"""Gets the project summary DTO"""

summary = await Project.get_project_summary(
Expand Down
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ UNDERPASS_URL=https://underpass.hotosm.org
#EXPORT TOOL Integration with 0(Disable) and 1(Enable) and S3 URL for Export Tool
#EXPORT_TOOL_S3_URL=https://foorawdataapi.s3.amazonaws.com
#ENABLE_EXPORT_TOOL=0
PROFILING=False

0 comments on commit 406e450

Please sign in to comment.