From f64f5725d16cea8ca3546941e5a36ee2b2709ceb Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 20 Feb 2020 17:48:48 +0000 Subject: [PATCH] Treat 400s from the api as internal server errors if we expect a 400 (for example, the api returns 400 if the service name is already in use) then we should handle that from the view function so we can correctly display a relevation error message to the user. But if it returns a 400 that we didn't expect (for example, because we sent variables of the wrong type through to an endpoint with a schema), then that is a bug that we should fix as any other raised exception. By treating it as a 500 we encourage users to report it to us, and also will get an alert email --- app/__init__.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c2921bafc..10c82a8a1 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -614,15 +614,10 @@ def register_errorhandlers(application): # noqa (C901 too complex) error.message )) error_code = error.status_code - if error_code == 400: - if isinstance(error.message, str): - msg = [error.message] - else: - msg = list(itertools.chain(*[error.message[x] for x in error.message.keys()])) - resp = make_response(render_template("error/400.html", message=msg)) - return useful_headers_after_request(resp) - elif error_code not in [401, 404, 403, 410]: - # probably a 500 or 503 + if error_code not in [401, 404, 403, 410]: + # probably a 500 or 503. + # it might be a 400, which we should handle as if it's an internal server error. If the API might + # legitimately return a 400, we should handle that within the view or the client that calls it. application.logger.exception("API {} failed with status {} message {}".format( error.response.url if error.response else 'unknown', error.status_code,