Repository: simphony/tornado-webapi

Issue:
Change BadRequest error to BadRepresentation
BadRequest is HTTP. It's technically a bad representation that the CRUD methods handle, and the exception should reflect that.

Migrated from https://github.com/simphony/simphony-remote/issues/181

Discussion:

Patch:
diff --git a/tornadowebapi/exceptions.py b/tornadowebapi/exceptions.py
index a5f3dd3..a2999b5 100644
--- a/tornadowebapi/exceptions.py
+++ b/tornadowebapi/exceptions.py
@@ -44,7 +44,7 @@ class NotFound(WebAPIException):
         return None
 
 
-class BadRequest(WebAPIException):
+class BadRepresentation(WebAPIException):
     """Exception raised when the resource representation is
     invalid or does not contain the appropriate keys.
     Raise this exception in your handlers when the received
@@ -53,6 +53,10 @@ class BadRequest(WebAPIException):
     http_code = httpstatus.BAD_REQUEST
 
 
+# Deprecated. Kept for compatibility
+BadRequest = BadRepresentation
+
+
 class Unable(WebAPIException):
     """Exception raised when the request cannot be performed
     for whatever reason that is not dependent on the client.
diff --git a/tox.ini b/tox.ini
index 1147235..debc066 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,2 +1,2 @@
 [flake8]
-exclude = build/*,venv/*,doc/source/conf.py
+exclude = build/*,venv*/*,doc/source/conf.py


Test patch:
diff --git a/tornadowebapi/tests/test_webapi.py b/tornadowebapi/tests/test_webapi.py
index 3a3c8e5..e3f3b8b 100644
--- a/tornadowebapi/tests/test_webapi.py
+++ b/tornadowebapi/tests/test_webapi.py
@@ -74,19 +74,19 @@ class UnsupportAll(Resource):
 class Unprocessable(Resource):
     @gen.coroutine
     def create(self, representation):
-        raise exceptions.BadRequest("unprocessable", foo="bar")
+        raise exceptions.BadRepresentation("unprocessable", foo="bar")
 
     @gen.coroutine
     def update(self, identifier, representation):
-        raise exceptions.BadRequest("unprocessable", foo="bar")
+        raise exceptions.BadRepresentation("unprocessable", foo="bar")
 
     @gen.coroutine
     def retrieve(self, identifier):
-        raise exceptions.BadRequest("unprocessable", foo="bar")
+        raise exceptions.BadRepresentation("unprocessable", foo="bar")
 
     @gen.coroutine
     def items(self):
-        raise exceptions.BadRequest("unprocessable", foo="bar")
+        raise exceptions.BadRepresentation("unprocessable", foo="bar")
 
 
 class UnsupportsCollection(Resource):
@@ -328,7 +328,7 @@ class TestREST(AsyncHTTPTestCase):
         self.assertEqual(res.code, httpstatus.BAD_REQUEST)
         self.assertEqual(res.headers["Content-Type"], 'application/json')
         self.assertEqual(escape.json_decode(res.body), {
-            "type": "BadRequest",
+            "type": "BadRepresentation",
             "message": "unprocessable",
             "foo": "bar",
         })
@@ -340,7 +340,7 @@ class TestREST(AsyncHTTPTestCase):
         self.assertEqual(res.code, httpstatus.BAD_REQUEST)
         self.assertEqual(res.headers["Content-Type"], 'application/json')
         self.assertEqual(escape.json_decode(res.body), {
-            "type": "BadRequest",
+            "type": "BadRepresentation",
             "message": "unprocessable",
             "foo": "bar",
         })
@@ -353,7 +353,7 @@ class TestREST(AsyncHTTPTestCase):
         self.assertEqual(res.code, httpstatus.BAD_REQUEST)
         self.assertEqual(res.headers["Content-Type"], 'application/json')
         self.assertEqual(escape.json_decode(res.body), {
-            "type": "BadRequest",
+            "type": "BadRepresentation",
             "message": "unprocessable",
             "foo": "bar",
         })
@@ -365,7 +365,7 @@ class TestREST(AsyncHTTPTestCase):
         self.assertEqual(res.code, httpstatus.BAD_REQUEST)
         self.assertEqual(res.headers["Content-Type"], 'application/json')
         self.assertEqual(escape.json_decode(res.body), {
-            "type": "BadRequest",
+            "type": "BadRepresentation",
             "message": "unprocessable",
             "foo": "bar",
         })
@@ -378,7 +378,7 @@ class TestREST(AsyncHTTPTestCase):
         self.assertEqual(res.code, httpstatus.BAD_REQUEST)
         self.assertEqual(res.headers["Content-Type"], 'application/json')
         self.assertEqual(escape.json_decode(res.body), {
-            "type": "BadRequest",
+            "type": "BadRepresentation",
             "message": "unprocessable",
             "foo": "bar",
         })