Skip to content

Commit 949ad81

Browse files
committed
Return 404 when cam not found
1 parent 2ab8f28 commit 949ad81

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

api/urls.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import httpx
22
from django.conf import settings
33
from django.db.models import Prefetch
4-
from django.shortcuts import render
4+
from django.shortcuts import get_object_or_404, render
55
from ninja import Field, NinjaAPI, Schema
66

77
from cams.models import Cam, Category
@@ -64,7 +64,10 @@ async def health(request):
6464

6565
@api.get("/cams/{cam_id}")
6666
async def cams_detail(request, cam_id: int):
67-
cam = await Cam.objects.aget(id=cam_id)
67+
try:
68+
cam = await Cam.objects.aget(id=cam_id)
69+
except Cam.DoesNotExist:
70+
return api.create_response(request, {"message": "Cam not found"}, status=404)
6871
async with httpx.AsyncClient() as client:
6972
fetcher = SurflineFetcher(cam.spot_id, client)
7073
tides, sunlight, wind, waves = await fetcher.fetch_all()

surfcamsapi/urls.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ async def get_full_detail(request, cam_id: str):
3636
cam = await Cam.objects.aget(slug=cam_id)
3737
except Cam.DoesNotExist:
3838
if cam_id.isdigit():
39-
cam = await Cam.objects.aget(id=cam_id)
39+
try:
40+
cam = await Cam.objects.aget(id=cam_id)
41+
except Cam.DoesNotExist:
42+
return HttpResponse("Cam not found", status=404)
4043
else:
41-
raise
44+
return HttpResponse("Cam not found", status=404)
4245
related_cams = await cam.related_cams()
4346

4447
return render(

surfline/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010

1111
async def get_surfline_data(request, cam_id: int):
12-
cam = await Cam.objects.aget(id=cam_id)
12+
try:
13+
cam = await Cam.objects.aget(id=cam_id)
14+
except Cam.DoesNotExist:
15+
return render(request, "surfline-error.html", {"message": "Cam not found"})
1316
async with httpx.AsyncClient() as client:
1417
fetcher = SurflineFetcher(cam.spot_id, client)
1518
try:

0 commit comments

Comments
 (0)