added error handling for DB exceptions

This commit is contained in:
2020-12-30 16:48:15 +11:00
parent e45e7633dd
commit 9beec7de86

21
main.py
View File

@@ -1,5 +1,6 @@
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import SQLAlchemyError
from flask_marshmallow import Marshmallow
from flask_bootstrap import Bootstrap
from wtforms import SubmitField, StringField, HiddenField, SelectField, IntegerField, TextAreaField, validators
@@ -416,14 +417,28 @@ def book(id):
st.SetMessage( "This is a parent book, cannot delete it without deleting sub books first" )
return redirect( '/book/{}'.format(book.id) )
else:
st.SetAlert("warning")
st.SetMessage("WARNING: Deleting being tested at present.<br>")
st.AppendMessage("Deleted {}".format(book.title) )
pid = 0
if len(book.parent) > 0:
pid = book.parent[0].id
db.session.delete(book)
db.session.commit()
st.SetAlert("warning")
try:
db.session.delete(book)
db.session.commit()
except SQLAlchemyError as e:
st.SetAlert( "danger" )
print("actual sql alchemy error")
st.SetMessage( e.orig )
return redirect( '/book/{}'.format(id) )
except Exception as e:
st.SetAlert( "danger" )
print("generic error")
if 'Dependency rule tried to blank-out primary key':
st.SetMessage( "<b>Failed to delete book:</b> The book has a link to an another table (probably a series) -- {} ".format( str(e) ) )
else:
st.SetMessage( "<b>Failed to delete book:</b>nbsp; {} ".format( str(e) ) )
return redirect( '/book/{}'.format(id) )
if pid > 0:
return redirect( '/book/{}'.format(pid) )
else: