Catalog seeding, blank-unique fix, slides rotation, console repair
This commit is contained in:
@@ -88,6 +88,12 @@ def create_app(config_name: str = None) -> Flask:
|
||||
# Initialize extensions
|
||||
init_extensions(app)
|
||||
|
||||
# An optional unique column accepts any number of NULLs but exactly one
|
||||
# empty string, so a second blank code collided and became a 500. Normalise
|
||||
# blank to NULL once, at the mapper, rather than in each endpoint.
|
||||
from .utils.blankunique import register_blank_unique_normaliser
|
||||
register_blank_unique_normaliser()
|
||||
|
||||
# Initialize plugin manager
|
||||
with app.app_context():
|
||||
plugin_manager.init_app(app, db)
|
||||
@@ -186,6 +192,31 @@ def register_error_handlers(app: Flask):
|
||||
from .utils.responses import error_response, ErrorCodes
|
||||
from .exceptions import ShopDBException
|
||||
|
||||
# A uniqueness violation is the caller's problem, not a server fault. Without
|
||||
# this it surfaced as a bare 500 with a SQLAlchemy traceback in the log and
|
||||
# nothing usable on screen - the operator saw "internal server error" for
|
||||
# having reused a code that was already taken.
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
@app.errorhandler(IntegrityError)
|
||||
def handle_integrity_error(error):
|
||||
from .extensions import db
|
||||
db.session.rollback()
|
||||
message = str(getattr(error, 'orig', error))
|
||||
app.logger.warning('integrity error: %s', message)
|
||||
if 'Duplicate entry' in message or 'UNIQUE constraint' in message:
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
'That value is already in use. Codes and identifiers must be unique.',
|
||||
http_code=409)
|
||||
if 'foreign key constraint' in message.lower():
|
||||
return error_response(
|
||||
ErrorCodes.VALIDATION_ERROR,
|
||||
'That record refers to something that does not exist, or is still in use elsewhere.',
|
||||
http_code=400)
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR,
|
||||
'The database rejected that change.', http_code=400)
|
||||
|
||||
@app.errorhandler(ShopDBException)
|
||||
def handle_shopdb_exception(error):
|
||||
http_codes = {
|
||||
|
||||
Reference in New Issue
Block a user