mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Catch itegrity errors and return 400.
When creating or updating an organisation an itegrity error is raise if the name is already used. This change adds a new error handler for the organisation to catch the named unique index and return a 400 with a sensible message. We have an other error handler for unique service names which was caught in the error handler for all blueprints. A new error handler for the service_blueprint has been created for catch those specific unique constraints. This is a nice way to encapulate the specific errors for a specific blueprint.
This commit is contained in:
@@ -3,7 +3,7 @@ from flask import (
|
||||
current_app,
|
||||
json)
|
||||
from notifications_utils.recipients import InvalidEmailError
|
||||
from sqlalchemy.exc import SQLAlchemyError, DataError
|
||||
from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from marshmallow import ValidationError
|
||||
from jsonschema import ValidationError as JsonSchemaValidationError
|
||||
@@ -95,20 +95,6 @@ def register_errors(blueprint):
|
||||
current_app.logger.info(e)
|
||||
return jsonify(result='error', message="No result found"), 404
|
||||
|
||||
@blueprint.errorhandler(SQLAlchemyError)
|
||||
def db_error(e):
|
||||
if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror and \
|
||||
('duplicate key value violates unique constraint "services_name_key"' in e.orig.pgerror or
|
||||
'duplicate key value violates unique constraint "services_email_from_key"' in e.orig.pgerror):
|
||||
return jsonify(
|
||||
result='error',
|
||||
message={'name': ["Duplicate service name '{}'".format(
|
||||
e.params.get('name', e.params.get('email_from', ''))
|
||||
)]}
|
||||
), 400
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message="Internal server error"), 500
|
||||
|
||||
# this must be defined after all other error handlers since it catches the generic Exception object
|
||||
@blueprint.app_errorhandler(500)
|
||||
@blueprint.errorhandler(Exception)
|
||||
|
||||
Reference in New Issue
Block a user