Repository: rorodata/firefly

Issue:
Incorrect HTTP Error Codes
When a request is made to a firefly server with data in invalid format like:

```
curl -i -d '{"a": [5 8]}' https://0.0.0.0:8080/predict
```
instead of
```
curl -i -d '{"a": [5, 8]}' https://0.0.0.0:8080/predict
```
the server returns `500 Internal Server Error`. But in such cases, the server should return `400 Bad Request` or `422 Unprocessable Entity` as the payload data is not in the required format.

Discussion:

Patch:
diff --git a/firefly/app.py b/firefly/app.py
index b53fced..3a22377 100644
--- a/firefly/app.py
+++ b/firefly/app.py
@@ -79,7 +79,11 @@ class FireflyFunction(object):
         if self.options.get("internal", False):
             return self.make_response(self.function())
 
-        kwargs = self.get_inputs(request)
+        try:
+            kwargs = self.get_inputs(request)
+        except ValueError as err:
+            return self.make_response({"error": str(err)}, status=400)
+
         try:
             validate_args(self.function, kwargs)
         except ValidationError as err:


Test patch:
diff --git a/tests/test_app.py b/tests/test_app.py
index 687cdc7..ab3db5c 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -88,6 +88,14 @@ class TestFireflyFunction:
         assert response.status == '200 OK'
         assert response.text == '9'
 
+    def test_call_for_bad_request(self):
+        def sum(a):
+            return sum(a)
+        func = FireflyFunction(sum)
+        request = Request.blank("/sum", POST='{"a": [3 8]}')
+        response = func(request)
+        assert response.status == '400 Bad Request'
+
     @py2_only
     def test_generate_signature(self):
         def sample_function(x, one="hey", two=None, **kwargs):