Michael Richardson avatar Michael Richardson committed 5187fc6

Don't throw a ValueError on an unknown emitter - instead, give back a nice error message.

Comments (0)

Files changed (2)

piston/resource.py

         except Exception, e:
             result = self.error_handler(e, request, meth)
 
-        emitter, ct = Emitter.get(em_format)
+        try:
+            emitter, ct = Emitter.get(em_format)
+        except ValueError:
+            response = rc.BAD_REQUEST
+            response.content = "Unknown format: %s" % em_format
+            return response
+
         fields = handler.fields
         if hasattr(handler, 'list_fields') and (
                 isinstance(result, list) or isinstance(result, QuerySet)):

tests/test_project/apps/testapp/tests.py

         resp = self.client.post('/api/issue58.json', outgoing, content_type='application/json',
                                 HTTP_AUTHORIZATION=self.auth_string)
         self.assertEquals(resp.status_code, 201)
-        
+
+
+class TestBadEmitter(MainTests):
+
+    def test_unknown_emitter(self):
+        data = {"msg": "hi!", "format": "json"}
+        response = self.client.get("/api/echo", data)
+        self.assertEqual(response.status_code, 200)
+
+        data["format"] = "xm"
+
+        response = self.client.get("/api/echo", data)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual("Unknown format: xm", response.content)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.