Add error handler for duplicate domain

This commit is contained in:
Chris Hill-Scott
2018-09-04 12:59:53 +01:00
parent 5d5047fb13
commit d52ee4606a
2 changed files with 45 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
from flask import Blueprint, jsonify, request
from flask import Blueprint, current_app, jsonify, request
from sqlalchemy.exc import IntegrityError
from app.dao.email_branding_dao import (
dao_create_email_branding,
@@ -18,6 +19,22 @@ email_branding_blueprint = Blueprint('email_branding', __name__)
register_errors(email_branding_blueprint)
@email_branding_blueprint.errorhandler(IntegrityError)
def handle_integrity_error(exc):
"""
Handle integrity errors caused by the unique constraint on domain
"""
if 'domain' in str(exc):
return jsonify(
result='error',
message={'name': ["Duplicate domain '{}'".format(
exc.params.get('domain')
)]}
), 400
current_app.logger.exception(exc)
return jsonify(result='error', message="Internal server error"), 500
@email_branding_blueprint.route('', methods=['GET'])
def get_email_branding_options():
email_branding_options = [o.serialize() for o in dao_get_email_branding_options()]