Skip to content

Commit e31aad6

Browse files
committed
validation exception handler
1 parent 825de31 commit e31aad6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

app/main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
app.main.py
33
"""
4-
import os
5-
import logging
64
import datetime as dt
5+
import logging
6+
import os
77
from typing import Dict, List
88

99
import fastapi
@@ -93,6 +93,18 @@ async def add_datasource(request: fastapi.Request, call_next):
9393
return response
9494

9595

96+
# ################
97+
# Exception Handler
98+
# ################
99+
100+
101+
@APP.exception_handler(pydantic.error_wrappers.ValidationError)
102+
async def handle_validation_error(
103+
request: fastapi.Request, exc: pydantic.error_wrappers.ValidationError
104+
):
105+
return fastapi.responses.JSONResponse({"message": exc.errors()}, status_code=422)
106+
107+
96108
# ################
97109
# Routes
98110
# ################
@@ -112,7 +124,7 @@ def get_latest(request: fastapi.Request):
112124

113125

114126
@APP.get("/locations", response_model=AllLocations)
115-
def get_all_locations(country_code: str = None, timelines: int = None):
127+
def get_all_locations(country_code: str = None, timelines: int = 0):
116128
return
117129

118130

0 commit comments

Comments
 (0)