Name an unknown circuit, layout or year with a 404 instead of a 500

The circuit and track routes walked circuits.json with chained lookups, so an unknown country, locality, circuit
or layout, or a year no layout covers, raised and answered 500 (/circuits/photos.json among them). One lookup now
names the first unknown part and the API answers it 404. numpy loads only where a script uses it, so the models,
and this lookup's test, need the standard library alone.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
2026-11-27 15:13:34 +01:00
parent db5c19f0d4
commit d514eb57a3
5 changed files with 106 additions and 44 deletions
+15 -28
View File
@@ -9,7 +9,7 @@ import logging
# Import our CircuitService
from app_info import APP_VERSION
from circuits_service import CircuitService, Format
from circuits_service import CircuitService, Format, NotFound
from posters_service import PostersService, Rendered
import poster_options
import poster_sources
@@ -220,37 +220,31 @@ async def get_countries():
"countries": [c.to_dict() for c in circuit_service.get_countries_list()]
}
@app.exception_handler(NotFound)
async def not_found(request: Request, error: NotFound) -> JSONResponse:
"""A country, locality, circuit or layout circuits.json does not hold, or a year no layout covers: named."""
return JSONResponse(status_code=404, content={"detail": str(error)})
@app.get("/circuits/{country_slug}")
async def get_cities(country_slug: str):
"""Get list of cities for a specific country"""
cities = circuit_service.get_localities_list(country_slug)
if not cities:
raise HTTPException(status_code=404, detail="Country not found")
return {"cities": [c.to_dict() for c in cities]}
return {"cities": [c.to_dict() for c in circuit_service.get_localities_list(country_slug)]}
@app.get("/circuits/{country_slug}/{city_slug}")
async def get_circuits(country_slug: str, city_slug: str):
"""Get list of circuits for a specific city"""
circuits = circuit_service.get_circuits_list(country_slug, city_slug)
if not circuits:
raise HTTPException(status_code=404, detail="City not found")
return {"circuits": [c.to_dict() for c in circuits]}
return {"circuits": [c.to_dict() for c in circuit_service.get_circuits_list(country_slug, city_slug)]}
@app.get("/circuits/{country_slug}/{city_slug}/{circuit_slug}")
async def get_circuit_details(country_slug: str, city_slug: str, circuit_slug: str):
"""Get circuit details and available formats"""
circuit_details = circuit_service.get_circuit_details(country_slug, city_slug, circuit_slug)
if not circuit_details:
raise HTTPException(status_code=404, detail="Circuit not found")
return circuit_details.to_dict()
return circuit_service.resolve(country_slug, city_slug, circuit_slug).to_dict()
@app.get("/circuits/{country_slug}/{city_slug}/{circuit_slug}/layout/{layout_slug}")
async def get_layout_details(country_slug: str, city_slug: str, circuit_slug: str, layout_slug: str):
"""Get details for a specific layout"""
layout_details = circuit_service.get_layout_details(country_slug, city_slug, circuit_slug, layout_slug)
if not layout_details:
raise HTTPException(status_code=404, detail="Layout not found")
return layout_details.to_dict()
return circuit_service.resolve(country_slug, city_slug, circuit_slug, layout_slug).to_dict()
@app.get("/tracks/{country_slug}/{city_slug}/{circuit_slug}/year/{year}/{image_format}")
async def get_track_by_year(
@@ -262,16 +256,8 @@ async def get_track_by_year(
response: Response
):
"""Get track layout for a specific year"""
circuit_details = circuit_service.get_circuit_details(country_slug, city_slug, circuit_slug)
if not circuit_details:
raise HTTPException(status_code=404, detail="Circuit not found")
try:
layout_slug = circuit_service.find_layout_slug_for_year(circuit_details, year)
return get_file_response(circuit_service.get_layout_details(country_slug, city_slug, circuit_slug, layout_slug), image_format, response)
except KeyError as e:
logger.error(f"KeyError in get_track_by_year: {str(e)}")
raise HTTPException(status_code=404, detail="Circuit or layout not found")
return get_file_response(circuit_service.layout_for_year(country_slug, city_slug, circuit_slug, year),
image_format, response)
@app.get("/circuits/{country_slug}/{city_slug}/{circuit_slug}/layout/{layout_slug}/{image_format}")
async def get_track_by_layout(
@@ -283,7 +269,8 @@ async def get_track_by_layout(
response: Response
):
"""Get specific track layout"""
return get_file_response(circuit_service.get_layout_details(country_slug, city_slug, circuit_slug, layout_slug), image_format, response)
return get_file_response(circuit_service.resolve(country_slug, city_slug, circuit_slug, layout_slug),
image_format, response)
@app.get("/grand-prix/{grand_prix_name}/{season}/{image_format}")
def get_grand_prix_circuit(grand_prix_name: str, season: int, image_format: Format, response: Response):