Преглед изворни кода

Add missing json error handling

Arkadiusz Ryś пре 2 година
родитељ
комит
9743ccfc73
4 измењених фајлова са 21 додато и 6 уклоњено
  1. 5 2
      mocka/routers/example.py
  2. 5 1
      mocka/routers/notch.py
  3. 6 1
      mocka/routers/octiva.py
  4. 5 2
      mocka/routers/template.py

+ 5 - 2
mocka/routers/example.py

@@ -1,4 +1,5 @@
 # https://fastapi.tiangolo.com/tutorial/body-multiple-params/#singular-values-in-body
+from json import JSONDecodeError
 from pathlib import Path
 from typing import Any
 import magic
@@ -29,8 +30,10 @@ class ExampleRouter(APIRouter):
                         return JSONResponse(status_code=400, content={"ctrl": "error"})
                     case "get":
                         return JSONResponse(status_code=200, content={"ctrl": "ok"})
-
-            body = await request.json()
+            try:
+                body = await request.json()
+            except JSONDecodeError:
+                return JSONResponse(status_code=500, content={"ctrl": "error", "debug": "Could not parse json input."})
             control = body.get("ctrl")
             arklog.debug(control)
             content = body.get("input").get("din").get("content")

+ 5 - 1
mocka/routers/notch.py

@@ -1,4 +1,5 @@
 import json
+from json import JSONDecodeError
 from pathlib import Path
 from typing import Any
 import arklog
@@ -81,7 +82,10 @@ class NotchRouter(APIRouter):
         @self.get("/simulation/")
         async def simulation_notch(request: Request, query: str | None = Query(None)) -> Response:
             """Notch simulation request response for a simulated activity."""
-            body = await request.json()
+            try:
+                body = await request.json()
+            except JSONDecodeError:
+                return JSONResponse(status_code=500, content={"ctrl": "error", "debug": "Could not parse json input."})
             control = body.get("ctrl")
             # The predefined activity expects the control to be on the cin or rep port.
             if control not in ["cin"]:

+ 6 - 1
mocka/routers/octiva.py

@@ -1,3 +1,5 @@
+from json import JSONDecodeError
+
 import cv2 as cv
 import numpy as np
 from pathlib import Path
@@ -65,7 +67,10 @@ class OctivaRouter(APIRouter):
         async def root_mock(request: Request, query: str | None = Query(None)) -> Response:
             """"""
             # TODO Change some of this to use buffers instead of files
-            body = await request.json()
+            try:
+                body = await request.json()
+            except JSONDecodeError:
+                return JSONResponse(status_code=500, content={"ctrl": "error", "debug": "Could not parse json input."})
             control = body.get("ctrl")
             # The predefined activity expects the control to be on the cin or rep port.
             if control not in ["cin", "rep"]:

+ 5 - 2
mocka/routers/template.py

@@ -1,3 +1,4 @@
+from json import JSONDecodeError
 from pathlib import Path
 from typing import Any
 import magic
@@ -27,8 +28,10 @@ class TemplateRouter(APIRouter):
                         return JSONResponse(status_code=400, content={"ctrl": "error"})
                     case "get":
                         return JSONResponse(status_code=200, content={"ctrl": "ok"})
-
-            body = await request.json()
+            try:
+                body = await request.json()
+            except JSONDecodeError:
+                return JSONResponse(status_code=500, content={"ctrl": "error", "debug": "Could not parse json input."})
             # Check if we were provided all the data we need to abort early
             control = body.get("ctrl")
             arklog.debug(control)