Kaynağa Gözat

add more checks for bad data

Daniel Sheffield 1 yıl önce
ebeveyn
işleme
a3e23dfac3
1 değiştirilmiş dosya ile 22 ekleme ve 2 silme
  1. 22 2
      rest/pyapi.py

+ 22 - 2
rest/pyapi.py

@@ -95,6 +95,7 @@ specified_schema = """
 </xsd:schema>
 """
 
+
 @route('/pyapi/random')
 def random():
     try:
@@ -105,6 +106,7 @@ def random():
     response.content_type = 'application/xhtml+xml; charset=utf-8'
     return f"{heading}{xml[0]}{random_schema}" + '\n'.join(xml[1:])
 
+
 @route('/pyapi/pg')
 def specified():
     kingdom = { 'a_kingdom', 'z_kingdom' }
@@ -112,12 +114,30 @@ def specified():
     translations = []
     references = []
     for c in categories:
-        r, t = request.query[c].split(' ',1)
+        try:
+            r, t = request.query[c].split(' ', 1)
+        except ValueError:
+            raise abort(
+                400,
+                f"Could not parse value for {c}."
+                f" Given: {request.query[c]}"
+            )
+
+        if None in (r or None, t or None,):
+            raise abort(
+                400,
+                f"Both reference and translation must be provided for {c}."
+                f" Given: {request.query[c]}"
+            )
         references.append(r)
         translations.append(t)
 
     if len(kingdom - set(categories)) not in (0, 2,):
-        raise abort(400, f"Both of {kingdom} must be specified or neither specified. Missing: {kingdom - set(categories)}")
+        raise abort(
+            400,
+            f"Both of {kingdom} must be specified or neither specified."
+            f" Missing: {kingdom - set(categories)}"
+        )
 
     try:
         with conn.cursor() as cur: