Allow formdata to handle errors better.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 8 Nov 2015 02:52:43 +0000 (03:52 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 8 Nov 2015 02:52:43 +0000 (03:52 +0100)
wrw/form.py

index e89901d..7905dc6 100644 (file)
@@ -7,9 +7,12 @@ def formparse(req):
     buf = {}
     buf.update(urllib.parse.parse_qsl(req.query))
     if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded":
-        rbody = req.input.read(2 ** 20)
+        try:
+            rbody = req.input.read(2 ** 20)
+        except IOError as exc:
+            return exc
         if len(rbody) >= 2 ** 20:
-            raise ValueError("x-www-form-urlencoded data is absurdly long")
+            return ValueError("x-www-form-urlencoded data is absurdly long")
         buf.update(urllib.parse.parse_qsl(rbody.decode("latin1")))
     return buf
 
@@ -161,5 +164,10 @@ class multipart(object):
         self.lastpart.parsehead(self.headcs)
         return self.lastpart
 
-def formdata(req):
-    return req.item(formparse)
+def formdata(req, onerror=Exception):
+    data = req.item(formparse)
+    if isinstance(data, Exception):
+        if onerror is Exception:
+            raise data
+        return onerror
+    return data