Repository: simphony/tornado-webapi

Issue:
Add Exists exception
In case we generate a resource that already exists for whatever reason

Discussion:

Patch:
diff --git a/tornadowebapi/exceptions.py b/tornadowebapi/exceptions.py
index 60bd639..64a3a2e 100644
--- a/tornadowebapi/exceptions.py
+++ b/tornadowebapi/exceptions.py
@@ -44,6 +44,19 @@ class NotFound(WebAPIException):
         return None
 
 
+class Exists(WebAPIException):
+    """Represents a case where the resource could not be created
+    because it already exists. This is generally raised in the
+    create() method if the resource has uniqueness constraints on
+    things other than the exposed id."""
+
+    http_code = httpstatus.CONFLICT
+
+    def representation(self):
+        """Exists does not have a representation, just an error status"""
+        return None
+
+
 class BadRepresentation(WebAPIException):
     """Exception raised when the resource representation is
     invalid or does not contain the appropriate keys.


Test patch:
diff --git a/tornadowebapi/tests/test_webapi.py b/tornadowebapi/tests/test_webapi.py
index e3f3b8b..0b5f7de 100644
--- a/tornadowebapi/tests/test_webapi.py
+++ b/tornadowebapi/tests/test_webapi.py
@@ -107,6 +107,12 @@ class Broken(Resource):
     items = boom
 
 
+class AlreadyPresent(Resource):
+    @gen.coroutine
+    def create(self, *args):
+        raise exceptions.Exists()
+
+
 class TestREST(AsyncHTTPTestCase):
     def setUp(self):
         super().setUp()
@@ -120,6 +126,7 @@ class TestREST(AsyncHTTPTestCase):
         registry.registry.register(Unprocessable)
         registry.registry.register(UnsupportsCollection)
         registry.registry.register(Broken)
+        registry.registry.register(AlreadyPresent)
         app = web.Application(handlers=handlers)
         app.hub = mock.Mock()
         return app
@@ -402,6 +409,12 @@ class TestREST(AsyncHTTPTestCase):
             method="GET")
         self.assertEqual(res.code, httpstatus.METHOD_NOT_ALLOWED)
 
+    def test_exists(self):
+        collection_url = "/api/v1/alreadypresents/"
+
+        res = self.fetch(collection_url, method="POST", body="{}")
+        self.assertEqual(res.code, httpstatus.CONFLICT)
+
 
 class TestRESTFunctions(unittest.TestCase):
     def test_api_handlers(self):