#92 Returned error messages in JSON
Merged by gnaponie. Opened by gnaponie.
gnaponie/waiverdb master  into  master

Download 92.patch

As in Greenwave now the error messages are in JSON format and not in HTML. Also fixed some errors in the doc.

I guess you could just do:
return insert_headers(response)

Can you split the doc changes into separate commit?

One less newline here to conform with pep?

rebased onto 30e174a086c1fbd2464e46051d7f119a533a6816

@gnaponie, there is some odd whitespace here. Can you remove it?

If your editor doesn't show it, you should be able to get at it with sed -i 's/ *$//g' waiverdb/app.py

The change looks reasonable to me and the test suite passes as is.

  • There's one cosmetic whitespace change I requested above.
  • I wonder how hard it would be to write an additional test for this to make sure it is working.

Something like this could help test that the 404 response is really JSON.

diff --git a/tests/test_api_v10.py b/tests/test_api_v10.py
index 6fe55e2..0ed1665 100644
--- a/tests/test_api_v10.py
+++ b/tests/test_api_v10.py
@@ -55,6 +55,11 @@ def test_get_waiver(client, session):
 def test_404_for_nonexistent_waiver(client, session):
     r = client.get('/api/v1.0/waivers/foo')
     assert r.status_code == 404
+    res_data = json.loads(r.get_data(as_text=True))
+    message = (
+        'The requested URL was not found on the server.  If you entered the '
+        'URL manually please check your spelling and try again.')
+    assert res_data['message'] == message
 def test_get_waivers(client, session):

Can you add that and then explore trying to add a whole new test that checks for a response with a status code of 500? You'll need to mock the app somehow to force it to raise an exception, which should then be caught by your handler to return a sane JSON response.

1 new commit added

  • Added test for 404 and 500 and json format

1 new commit added

  • Added more information for setup environment

Looks good to me. :+1:

Pull-Request has been merged by gnaponie

Alternatively, you could use pytest.raises which might be nicer.

https://docs.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions

@mjia but as far as I know I do not have an api returning status code 500... I can always create one, but is that the correct way?

@gnaponie, yeah, my bad as I misread the code. Don't worry, :-)

@mjia thank you anyway for your comment! :) I've learnt something new.

Metadata