added error handling for DB exceptions
This commit is contained in:
21
main.py
21
main.py
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user