Skip to content

Commit

Permalink
Send 400 for BadDataError. (#202). r=rail
Browse files Browse the repository at this point in the history
  • Loading branch information
bhearsum committed Dec 29, 2016
1 parent e911eda commit 9a8f9d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 3 additions & 4 deletions auslib/test/web/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,11 @@ def testEmptySnippetOn500(self):
self.assertEqual('I break!', str(exc.exception.message))

def testSentryBadDataError(self):
with mock.patch("auslib.web.views.client.ClientRequestView.get") as m:
with mock.patch("auslib.web.views.client.ClientRequestView.get") as m, mock.patch("auslib.web.base.sentry") as sentry:
m.side_effect = BadDataError("exterminate!")
with mock.patch("auslib.web.base.sentry") as sentry, self.assertRaises(Exception) as exc:
self.client.get("/update/4/b/1.0/1/p/l/a/a/a/a/1/update.xml")
ret = self.client.get("/update/4/b/1.0/1/p/l/a/a/a/a/1/update.xml")
self.assertFalse(sentry.captureException.called)
self.assertEqual('exterminate!', str(exc.exception.message))
self.assertEqual(ret.status_code, 400, ret.data)

def testSentryRealError(self):
with mock.patch("auslib.web.views.client.ClientRequestView.get") as m:
Expand Down
14 changes: 8 additions & 6 deletions auslib/web/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
log = logging.getLogger(__name__)

from flask import Flask, make_response, send_from_directory, abort
from flask import Flask, make_response, send_from_directory, abort, Response

from raven.contrib.flask import Sentry

Expand Down Expand Up @@ -37,13 +37,15 @@ def fourohfour(error):
@app.errorhandler(Exception)
def generic(error):
"""Deals with any unhandled exceptions. If the exception is not a
BadDataError, it will be sent to Sentry. Regardless of the exception,
a 500 response is returned."""
BadDataError, it will be sent to Sentry, and a 400 will be returned,
because BadDataErrors are considered to be the client's fault.
Otherwise, the error is just re-raised (which causes a 500)."""

if not isinstance(error, BadDataError):
if sentry.client:
sentry.captureException()
if isinstance(error, BadDataError):
return Response(status=400, response=error.message)

if sentry.client:
sentry.captureException()
raise error


Expand Down

0 comments on commit 9a8f9d9

Please sign in to comment.