From 4379669cf89b440ecd83d562966d290d9a6ac9b3 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 12 Mar 2022 17:14:54 +0100 Subject: [PATCH] Database error is more detailed renamed debug_or_exception to error_or_exception --- cps/admin.py | 34 ++++++++++++------------- cps/db.py | 8 +++--- cps/editbooks.py | 23 ++++++++++------- cps/gdrive.py | 2 +- cps/gdriveutils.py | 2 +- cps/helper.py | 4 +-- cps/logger.py | 2 +- cps/metadata_provider/amazon.py | 4 +-- cps/oauth_bb.py | 4 +-- cps/services/worker.py | 2 +- cps/shelf.py | 44 ++++++++++++++++----------------- cps/tasks/mail.py | 10 ++++---- cps/ub.py | 2 +- cps/updater.py | 2 +- cps/web.py | 4 +-- 15 files changed, 76 insertions(+), 71 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 18bd87ec..60a674e8 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -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): @@ -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: @@ -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): @@ -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 "" @@ -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"): @@ -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: @@ -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) diff --git a/cps/db.py b/cps/db.py index 6ff0d03e..2292614c 100644 --- a/cps/db.py +++ b/cps/db.py @@ -597,7 +597,7 @@ class CalibreDB(): 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, @@ -769,7 +769,7 @@ class CalibreDB(): 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 @@ -792,7 +792,7 @@ class CalibreDB(): 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: @@ -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() diff --git a/cps/editbooks.py b/cps/editbooks.py index 7e251696..3a08f061 100755 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -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), @@ -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 @@ -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 @@ -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)) @@ -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') @@ -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') @@ -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: diff --git a/cps/gdrive.py b/cps/gdrive.py index e782cb9e..60e3d47b 100644 --- a/cps/gdrive.py +++ b/cps/gdrive.py @@ -152,7 +152,7 @@ try: 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 diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 57f272ff..0b704db4 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -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() diff --git a/cps/helper.py b/cps/helper.py index 03a2d03b..b0b28a99 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -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) @@ -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') diff --git a/cps/logger.py b/cps/logger.py index 053d0bd3..fcc25c27 100644 --- a/cps/logger.py +++ b/cps/logger.py @@ -42,7 +42,7 @@ logging.addLevelName(logging.CRITICAL, "CRIT") 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) diff --git a/cps/metadata_provider/amazon.py b/cps/metadata_provider/amazon.py index acea7e07..73c7e1fc 100644 --- a/cps/metadata_provider/amazon.py +++ b/cps/metadata_provider/amazon.py @@ -105,7 +105,7 @@ class Amazon(Metadata): match.cover = "" return match, index except Exception as e: - log.debug_or_exception(e) + log.error_or_exception(e) return val = list() @@ -126,5 +126,5 @@ class Amazon(Metadata): 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 [] diff --git a/cps/oauth_bb.py b/cps/oauth_bb.py index d9efd41e..d9a60c0e 100644 --- a/cps/oauth_bb.py +++ b/cps/oauth_bb.py @@ -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") @@ -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: diff --git a/cps/services/worker.py b/cps/services/worker.py index 076c9104..aea6d640 100644 --- a/cps/services/worker.py +++ b/cps/services/worker.py @@ -178,7 +178,7 @@ class CalibreTask: 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() diff --git a/cps/shelf.py b/cps/shelf.py index 27939cdc..0bf12164 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -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: @@ -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") @@ -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: @@ -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, @@ -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')) @@ -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: @@ -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, diff --git a/cps/tasks/mail.py b/cps/tasks/mail.py index 80adbbcc..4954c2d6 100755 --- a/cps/tasks/mail.py +++ b/cps/tasks/mail.py @@ -167,10 +167,10 @@ class TaskEmail(CalibreTask): else: self.send_gmail_email(msg) except MemoryError as e: - log.debug_or_exception(e, stacklevel=3) + log.error_or_exception(e, stacklevel=3) self._handleError(u'MemoryError sending e-mail: {}'.format(str(e))) except (smtplib.SMTPException, smtplib.SMTPAuthenticationError) as e: - log.debug_or_exception(e, stacklevel=3) + log.error_or_exception(e, stacklevel=3) if hasattr(e, "smtp_error"): text = e.smtp_error.decode('utf-8').replace("\n", '. ') elif hasattr(e, "message"): @@ -181,10 +181,10 @@ class TaskEmail(CalibreTask): text = '' self._handleError(u'Smtplib Error sending e-mail: {}'.format(text)) except (socket.error) as e: - log.debug_or_exception(e, stacklevel=3) + log.error_or_exception(e, stacklevel=3) self._handleError(u'Socket Error sending e-mail: {}'.format(e.strerror)) except Exception as ex: - log.debug_or_exception(ex, stacklevel=3) + log.error_or_exception(ex, stacklevel=3) self._handleError(u'Error sending e-mail: {}'.format(ex)) def send_standard_email(self, msg): @@ -257,7 +257,7 @@ class TaskEmail(CalibreTask): data = file_.read() file_.close() except IOError as e: - log.debug_or_exception(e, stacklevel=3) + log.error_or_exception(e, stacklevel=3) log.error(u'The requested file could not be read. Maybe wrong permissions?') return None return data diff --git a/cps/ub.py b/cps/ub.py index 786dc909..a2be004c 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -853,5 +853,5 @@ def session_commit(success=None, _session=None): log.info(success) except (exc.OperationalError, exc.InvalidRequestError) as e: s.rollback() - log.debug_or_exception(e) + log.error_or_exception(e) return "" diff --git a/cps/updater.py b/cps/updater.py index 181c4374..f7341002 100644 --- a/cps/updater.py +++ b/cps/updater.py @@ -117,7 +117,7 @@ class Updater(threading.Thread): except (IOError, OSError) as ex: self.status = 12 log.error(u'Possible Reason for error: update file could not be saved in temp dir') - log.debug_or_exception(ex) + log.error_or_exception(ex) self.pause() return False diff --git a/cps/web.py b/cps/web.py index 94f511b1..e3ad4719 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1375,7 +1375,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): try: q = adv_search_custom_columns(cc, term, q) except AttributeError as ex: - log.debug_or_exception(ex) + log.error_or_exception(ex) flash(_("Error on search for custom columns, please restart Calibre-Web"), category="error") q = q.order_by(*sort_param).all() @@ -1437,7 +1437,7 @@ def serve_book(book_id, book_format, anyname): df = getFileFromEbooksFolder(book.path, data.name + "." + book_format) return do_gdrive_download(df, headers, (book_format.upper() == 'TXT')) except AttributeError as ex: - log.debug_or_exception(ex) + log.error_or_exception(ex) return "File Not Found" else: if book_format.upper() == 'TXT':