mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
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:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user