make integrity check more restrictive

previously, it was too loose - checking `"name" in str(exc)` returns
false positives.

By changing from three if statements to a loop we can cut down on
unnecessary code (and ensure that the returned objects are consistent),
and by using the full check constraint name we can be sure that we're
only capturing exactly the right errors. Additionally, don't return
the original data in the error message - it's obvious what the name is
because it'll be populated in the form you just filled in.
This commit is contained in:
Leo Hemsted
2019-01-28 17:01:13 +00:00
parent 4dbb54b5e7
commit 1cd498dd39
2 changed files with 13 additions and 28 deletions

View File

@@ -22,27 +22,12 @@ def handle_integrity_error(exc):
"""
Handle integrity errors caused by the unique constraint
"""
if 'domain' in str(exc):
return jsonify(
result='error',
message={'name': ["Duplicate domain '{}'".format(
exc.params.get('domain')
)]}
), 400
if 'name' in str(exc):
return jsonify(
result='error',
message={'name': ["Duplicate name '{}'".format(
exc.params.get('name')
)]}
), 400
if 'filename' in str(exc):
return jsonify(
result='error',
message={'name': ["Duplicate filename '{}'".format(
exc.params.get('fileaname')
)]}
), 400
for col in {'domain', 'name', 'filename'}:
if 'letter_branding_{}_key'.format(col) in str(exc):
return jsonify(
result='error',
message={col: ["{} already in use".format(col.title())]}
), 400
current_app.logger.exception(exc)
return jsonify(result='error', message="Internal server error"), 500