Skip to content

Commit 8b5f292

Browse files
committed
feat: updated error handling for tiles endpoint
1 parent 9ac6d09 commit 8b5f292

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

app/middleware/error_handling.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Any
12
from fastapi import Request, status
23
from fastapi.exceptions import RequestValidationError
34
from fastapi.responses import JSONResponse
@@ -40,15 +41,24 @@ async def generic_exception_handler(request: Request, exc: Exception):
4041
)
4142

4243

44+
def _parse_validation_error(err: Any):
45+
if "ctx" in err:
46+
del err["ctx"]
47+
return err
48+
49+
4350
async def validation_exception_handler(request: Request, exc: RequestValidationError):
4451

52+
logger.error(f"Request validation error: {exc.__class__.__name__}: {exc}")
4553
content = ErrorResponse(
4654
error_code="VALIDATION_ERROR",
4755
message="Request validation failed.",
48-
details={"errors": exc.errors()},
56+
details={"errors": [_parse_validation_error(error) for error in exc.errors()]},
4957
request_id=correlation_id_ctx.get(),
5058
)
5159

60+
logger.error(content.dict())
61+
5262
return JSONResponse(
5363
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content=content.dict()
5464
)

app/routers/tiles.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from geojson_pydantic import GeometryCollection, Polygon
44
from loguru import logger
55

6+
from app.error import DispatcherException, ErrorResponse, InternalException
7+
from app.middleware.error_handling import get_dispatcher_error_response
68
from app.schemas.tiles import GridTypeEnum, TileRequest
79
from app.services.tiles.base import split_polygon_by_grid
810

@@ -54,7 +56,18 @@
5456
}
5557
}
5658
},
57-
}
59+
},
60+
InternalException.http_status: {
61+
"description": "Internal server error",
62+
"model": ErrorResponse,
63+
"content": {
64+
"application/json": {
65+
"example": get_dispatcher_error_response(
66+
InternalException(), "request-id"
67+
)
68+
}
69+
},
70+
},
5871
},
5972
)
6073
def split_in_tiles(
@@ -90,11 +103,12 @@ def split_in_tiles(
90103
try:
91104
logger.debug(f"Splitting tiles in a {payload.grid} formation")
92105
return split_polygon_by_grid(payload.aoi, payload.grid)
106+
except DispatcherException as de:
107+
raise de
93108
except Exception as e:
94-
logger.exception(
95-
f"An error occurred while calculating tiles for {payload.grid}"
109+
logger.error(
110+
f"An error occurred while calculating tiles for {payload.grid}: {e}"
96111
)
97-
raise HTTPException(
98-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
99-
detail=f"An error occurred while calculating tiles for {payload.grid}: {e}",
112+
raise InternalException(
113+
message=f"An error occurred while calculating tiles for {payload.grid}"
100114
)

0 commit comments

Comments
 (0)