Skip to content

Commit

Permalink
Database error is more detailed
Browse files Browse the repository at this point in the history
renamed debug_or_exception to error_or_exception
  • Loading branch information
OzzieIsaacs committed Mar 12, 2022
1 parent 2b31b6a commit 4379669
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 71 deletions.
34 changes: 17 additions & 17 deletions cps/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,10 +1216,10 @@ def _db_configuration_update_helper():

# gdrive_error drive setup
gdrive_error = _configuration_gdrive_helper(to_save)
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
_db_configuration_result(_("Settings DB is not Writeable"), gdrive_error)
log.error_or_exception("Settings Database error: {}".format(e))
_db_configuration_result(_(u"Database error: %(error)s.", error=e.orig), gdrive_error)
try:
metadata_db = os.path.join(to_save['config_calibre_dir'], "metadata.db")
if config.config_use_google_drive and is_gdrive_ready() and not os.path.exists(metadata_db):
Expand Down Expand Up @@ -1332,10 +1332,10 @@ def _configuration_update_helper():
unrar_status = helper.check_unrar(config.config_rarfile_location)
if unrar_status:
return _configuration_result(unrar_status)
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
_configuration_result(_("Settings DB is not Writeable"))
log.error_or_exception("Settings Database error: {}".format(e))
_configuration_result(_(u"Database error: %(error)s.", error=e.orig))

config.save()
if reboot_required:
Expand Down Expand Up @@ -1430,10 +1430,10 @@ def _handle_new_user(to_save, content, languages, translations, kobo_support):
ub.session.rollback()
log.error("Found an existing account for {} or {}".format(content.name, content.email))
flash(_("Found an existing account for this e-mail address or name."), category="error")
except OperationalError:
except OperationalError as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")


def _delete_user(content):
Expand Down Expand Up @@ -1547,10 +1547,10 @@ def _handle_edit_user(to_save, content, languages, translations, kobo_support):
ub.session.rollback()
log.error("An unknown error occurred while changing user: {}".format(str(ex)))
flash(_(u"An unknown error occurred. Please try again later."), category="error")
except OperationalError:
except OperationalError as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return ""


Expand Down Expand Up @@ -1616,10 +1616,10 @@ def update_mailsettings():
_config_int(to_save, "mail_size", lambda y: int(y)*1024*1024)
try:
config.save()
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return edit_mailsettings()

if to_save.get("test"):
Expand Down Expand Up @@ -1851,7 +1851,7 @@ def import_ldap_users():
try:
new_users = services.ldap.get_group_members(config.config_ldap_group_name)
except (services.ldap.LDAPException, TypeError, AttributeError, KeyError) as e:
log.debug_or_exception(e)
log.error_or_exception(e)
showtext['text'] = _(u'Error: %(ldaperror)s', ldaperror=e)
return json.dumps(showtext)
if not new_users:
Expand Down Expand Up @@ -1879,7 +1879,7 @@ def import_ldap_users():
try:
user_data = services.ldap.get_object_details(user=user_identifier, query_filter=query_filter)
except AttributeError as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
continue
if user_data:
user_count, message = ldap_import_create_user(user, user_data)
Expand Down
8 changes: 4 additions & 4 deletions cps/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def setup_db(cls, config_calibre_dir, app_db_path):
cc = conn.execute(text("SELECT id, datatype FROM custom_columns"))
cls.setup_db_cc_classes(cc)
except OperationalError as e:
log.debug_or_exception(e)
log.error_or_exception(e)

cls.session_factory = scoped_session(sessionmaker(autocommit=False,
autoflush=True,
Expand Down Expand Up @@ -769,7 +769,7 @@ def fill_indexpage_with_archived_books(self, page, database, pagesize, db_filter
len(query.all()))
entries = query.order_by(*order).offset(off).limit(pagesize).all()
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
# display authors in right order
entries = self.order_authors(entries, True, join_archive_read)
return entries, randm, pagination
Expand All @@ -792,7 +792,7 @@ def order_authors(self, entries, list_return=False, combined=False):
results = self.session.query(Authors).filter(Authors.sort == auth.lstrip().strip()).all()
# ToDo: How to handle not found author name
if not len(results):
log.error("Author {} not found to display name in right order".format(auth))
log.error("Author {} not found to display name in right order".format(auth.strip()))
# error = True
break
for r in results:
Expand Down Expand Up @@ -974,5 +974,5 @@ def lcase(s):
return unidecode.unidecode(s.lower())
except Exception as ex:
log = logger.create()
log.debug_or_exception(ex)
log.error_or_exception(ex)
return s.lower()
23 changes: 14 additions & 9 deletions cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def delete_book_from_table(book_id, book_format, jsonResponse):
kobo_sync_status.remove_synced_book(book.id, True)
calibre_db.session.commit()
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
calibre_db.session.rollback()
if jsonResponse:
return json.dumps([{"location": url_for("editbook.edit_book", book_id=book_id),
Expand Down Expand Up @@ -663,8 +663,8 @@ def upload_single_file(request, book, book_id):
calibre_db.update_title_sort(config)
except (OperationalError, IntegrityError) as e:
calibre_db.session.rollback()
log.error('Database error: %s', e)
flash(_(u"Database error: %(error)s.", error=e), category="error")
log.error_or_exception("Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return redirect(url_for('web.show_book', book_id=book.id))

# Queue uploader info
Expand Down Expand Up @@ -756,7 +756,7 @@ def edit_book(book_id):
try:
calibre_db.update_title_sort(config)
except sqliteOperationalError as e:
log.debug_or_exception(e)
log.error_or_exception(e)
calibre_db.session.rollback()

# Show form
Expand Down Expand Up @@ -864,8 +864,13 @@ def edit_book(book_id):
calibre_db.session.rollback()
flash(str(e), category="error")
return redirect(url_for('web.show_book', book_id=book.id))
except (OperationalError, IntegrityError) as e:
log.error_or_exception("Database error: {}".format(e))
calibre_db.session.rollback()
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return redirect(url_for('web.show_book', book_id=book.id))
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
calibre_db.session.rollback()
flash(_("Error editing book, please check logfile for details"), category="error")
return redirect(url_for('web.show_book', book_id=book.id))
Expand Down Expand Up @@ -1103,8 +1108,8 @@ def upload():
return Response(json.dumps(resp), mimetype='application/json')
except (OperationalError, IntegrityError) as e:
calibre_db.session.rollback()
log.error("Database error: %s", e)
flash(_(u"Database error: %(error)s.", error=e), category="error")
log.error_or_exception("Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')


Expand Down Expand Up @@ -1234,7 +1239,7 @@ def edit_list_book(param):
calibre_db.session.commit()
except (OperationalError, IntegrityError) as e:
calibre_db.session.rollback()
log.error("Database error: {}".format(e))
log.error_or_exception("Database error: {}".format(e))
ret = Response(json.dumps({'success': False,
'msg': 'Database error: {}'.format(e.orig)}),
mimetype='application/json')
Expand Down Expand Up @@ -1344,7 +1349,7 @@ def table_xchange_author_title():
calibre_db.session.commit()
except (OperationalError, IntegrityError) as e:
calibre_db.session.rollback()
log.error("Database error: %s", e)
log.error_or_exception("Database error: %s", e)
return json.dumps({'success': False})

if config.config_use_google_drive:
Expand Down
2 changes: 1 addition & 1 deletion cps/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def on_received_watch_confirmation():
move(os.path.join(tmp_dir, "tmp_metadata.db"), dbpath)
calibre_db.reconnect_db(config, ub.app_DB_path)
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
return ''
except AttributeError:
pass
2 changes: 1 addition & 1 deletion cps/gdriveutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def getDrive(drive=None, gauth=None):
except RefreshError as e:
log.error("Google Drive error: %s", e)
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
else:
# Initialize the saved creds
gauth.Authorize()
Expand Down
4 changes: 2 additions & 2 deletions cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def get_book_cover_internal(book, use_generic_cover_on_failure):
log.error('%s/cover.jpg not found on Google Drive', book.path)
return get_cover_on_failure(use_generic_cover_on_failure)
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
return get_cover_on_failure(use_generic_cover_on_failure)
else:
cover_file_path = os.path.join(config.config_calibre_dir, book.path)
Expand Down Expand Up @@ -861,7 +861,7 @@ def check_unrar(unrarLocation):
log.debug("unrar version %s", version)

except (OSError, UnicodeDecodeError) as err:
log.debug_or_exception(err)
log.error_or_exception(err)
return _('Error excecuting UnRar')


Expand Down
2 changes: 1 addition & 1 deletion cps/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

class _Logger(logging.Logger):

def debug_or_exception(self, message, stacklevel=2, *args, **kwargs):
def error_or_exception(self, message, stacklevel=2, *args, **kwargs):
if sys.version_info > (3, 7):
if is_debug_enabled():
self.exception(message, stacklevel=stacklevel, *args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions cps/metadata_provider/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def inner(link, index) -> [dict, int]:
match.cover = ""
return match, index
except Exception as e:
log.debug_or_exception(e)
log.error_or_exception(e)
return

val = list()
Expand All @@ -126,5 +126,5 @@ def inner(link, index) -> [dict, int]:
result = list(filter(lambda x: x, val))
return [x[0] for x in sorted(result, key=itemgetter(1))] #sort by amazons listing order for best relevance
except requests.exceptions.HTTPError as e:
log.debug_or_exception(e)
log.error_or_exception(e)
return []
4 changes: 2 additions & 2 deletions cps/oauth_bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def bind_oauth_or_register(provider_id, provider_user_id, redirect_url, provider
log.info("Link to {} Succeeded".format(provider_name))
return redirect(url_for('web.profile'))
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
ub.session.rollback()
else:
flash(_(u"Login failed, No User Linked With OAuth Account"), category="error")
Expand Down Expand Up @@ -197,7 +197,7 @@ def unlink_oauth(provider):
flash(_(u"Unlink to %(oauth)s Succeeded", oauth=oauth_check[provider]), category="success")
log.info("Unlink to {} Succeeded".format(oauth_check[provider]))
except Exception as ex:
log.debug_or_exception(ex)
log.error_or_exception(ex)
ub.session.rollback()
flash(_(u"Unlink to %(oauth)s Failed", oauth=oauth_check[provider]), category="error")
except NoResultFound:
Expand Down
2 changes: 1 addition & 1 deletion cps/services/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def start(self, *args):
self.run(*args)
except Exception as ex:
self._handleError(str(ex))
log.debug_or_exception(ex)
log.error_or_exception(ex)

self.end_time = datetime.now()

Expand Down
44 changes: 22 additions & 22 deletions cps/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def add_to_shelf(shelf_id, book_id):
try:
ub.session.merge(shelf)
ub.session.commit()
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_(u"Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
if "HTTP_REFERER" in request.environ:
return redirect(request.environ["HTTP_REFERER"])
else:
Expand Down Expand Up @@ -154,10 +154,10 @@ def search_to_shelf(shelf_id):
ub.session.merge(shelf)
ub.session.commit()
flash(_(u"Books have been added to shelf: %(sname)s", sname=shelf.name), category="success")
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
else:
log.error("Could not add books to shelf: {}".format(shelf.name))
flash(_(u"Could not add books to shelf: %(sname)s", sname=shelf.name), category="error")
Expand Down Expand Up @@ -197,10 +197,10 @@ def remove_from_shelf(shelf_id, book_id):
ub.session.delete(book_shelf)
shelf.last_modified = datetime.utcnow()
ub.session.commit()
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
if "HTTP_REFERER" in request.environ:
return redirect(request.environ["HTTP_REFERER"])
else:
Expand Down Expand Up @@ -273,12 +273,12 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False):
return redirect(url_for('shelf.show_shelf', shelf_id=shelf.id))
except (OperationalError, InvalidRequestError) as ex:
ub.session.rollback()
log.debug_or_exception(ex)
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception(ex)
log.error_or_exception("Settings Database error: {}".format(ex))
flash(_(u"Database error: %(error)s.", error=ex.orig), category="error")
except Exception as ex:
ub.session.rollback()
log.debug_or_exception(ex)
log.error_or_exception(ex)
flash(_(u"There was an error"), category="error")
return render_title_template('shelf_edit.html',
shelf=shelf,
Expand Down Expand Up @@ -337,10 +337,10 @@ def delete_shelf(shelf_id):
flash(_("Error deleting Shelf"), category="error")
else:
flash(_("Shelf successfully deleted"), category="success")
except InvalidRequestError:
except InvalidRequestError as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")
return redirect(url_for('web.index'))


Expand Down Expand Up @@ -374,10 +374,10 @@ def order_shelf(shelf_id):
# if order diffrent from before -> shelf.last_modified = datetime.utcnow()
try:
ub.session.commit()
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")

result = list()
if shelf:
Expand Down Expand Up @@ -450,10 +450,10 @@ def render_show_shelf(shelf_type, shelf_id, page_no, sort_param):
try:
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == entry.book_id).delete()
ub.session.commit()
except (OperationalError, InvalidRequestError):
except (OperationalError, InvalidRequestError) as e:
ub.session.rollback()
log.error("Settings DB is not Writeable")
flash(_("Settings DB is not Writeable"), category="error")
log.error_or_exception("Settings Database error: {}".format(e))
flash(_(u"Database error: %(error)s.", error=e.orig), category="error")

return render_title_template(page,
entries=result,
Expand Down
Loading

0 comments on commit 4379669

Please sign in to comment.