Error logging and mutables as function defaults (#930)

* Remove mutable iterables as function defaults
* Improve error reporting and standardise how it's done
This commit is contained in:
Veronica Berglyd Olsen
2021-11-10 00:32:01 +01:00
committed by GitHub
parent 67d2e266fd
commit 00eea24e3f
15 changed files with 88 additions and 65 deletions
+15 -11
View File
@@ -45,6 +45,7 @@ from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog
from novelwriter.core import ToHtml, ToOdt, ToMarkdown
from novelwriter.enum import nwAlert, nwItemType, nwItemLayout, nwItemClass
from novelwriter.error import formatException, logException
from novelwriter.common import fuzzyTime, makeFileNameSafe
from novelwriter.constants import nwConst, nwFiles
from novelwriter.gui.custom import QSwitch
@@ -727,7 +728,7 @@ class GuiBuildNovel(QDialog):
except Exception:
logger.error("Failed to build document '%s'", tItem.itemHandle)
novelwriter.logException()
logException()
if isPreview:
self.docView.setText((
"Failed to generate preview. "
@@ -873,7 +874,7 @@ class GuiBuildNovel(QDialog):
makeOdt.saveOpenDocText(savePath)
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt == self.FMT_FODT:
makeOdt = ToOdt(self.theProject, isFlat=True)
@@ -882,7 +883,7 @@ class GuiBuildNovel(QDialog):
makeOdt.saveFlatXML(savePath)
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt == self.FMT_HTM:
makeHtml = ToHtml(self.theProject)
@@ -894,7 +895,7 @@ class GuiBuildNovel(QDialog):
makeHtml.saveHTML5(savePath)
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt == self.FMT_NWD:
makeNwd = ToMarkdown(self.theProject)
@@ -907,7 +908,7 @@ class GuiBuildNovel(QDialog):
makeNwd.saveRawMarkdown(savePath)
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt in (self.FMT_MD, self.FMT_GH):
makeMd = ToMarkdown(self.theProject)
@@ -924,7 +925,7 @@ class GuiBuildNovel(QDialog):
makeMd.saveMarkdown(savePath)
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt == self.FMT_JSON_H or theFmt == self.FMT_JSON_M:
jsonData = {
@@ -969,7 +970,7 @@ class GuiBuildNovel(QDialog):
outFile.write(json.dumps(jsonData, indent=2))
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
elif theFmt == self.FMT_PDF:
try:
@@ -984,7 +985,7 @@ class GuiBuildNovel(QDialog):
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
else:
# If the if statements above and here match, it should not
@@ -1050,7 +1051,7 @@ class GuiBuildNovel(QDialog):
theData = json.loads(theJson)
except Exception:
logger.error("Failed to load build cache")
novelwriter.logException()
logException()
return False
if "buildTime" in theData.keys():
@@ -1078,7 +1079,7 @@ class GuiBuildNovel(QDialog):
}, indent=2))
except Exception:
logger.error("Failed to save build cache")
novelwriter.logException()
logException()
return False
return True
@@ -1302,9 +1303,12 @@ class GuiBuildNovelDocView(QTextBrowser):
return
def setStyleSheet(self, theStyles=[]):
def setStyleSheet(self, theStyles=None):
"""Set the stylesheet for the preview document.
"""
if theStyles is None:
theStyles = []
if not theStyles:
theStyles.append("h1, h2 {color: rgb(66, 113, 174);}")
theStyles.append("h3, h4 {color: rgb(50, 50, 50);}")
+5 -4
View File
@@ -38,6 +38,7 @@ from PyQt5.QtWidgets import (
)
from novelwriter.enum import nwAlert
from novelwriter.error import formatException
from novelwriter.common import formatTime, checkInt, checkIntRange, checkIntTuple
from novelwriter.constants import nwConst, nwFiles
from novelwriter.gui.custom import QSwitch
@@ -404,7 +405,7 @@ class GuiWritingStats(QDialog):
wSuccess = True
except Exception as exc:
errMsg = str(exc)
errMsg = formatException(exc)
wSuccess = False
# Report to user
@@ -480,9 +481,9 @@ class GuiWritingStats(QDialog):
self.logData.append((dStart, sDiff, wcNovel, wcNotes, sIdle))
except Exception as exc:
self.theParent.makeAlert([
self.tr("Failed to read session log file."), str(exc)
], nwAlert.ERROR)
self.theParent.makeAlert(self.tr(
"Failed to read session log file."
), nwAlert.ERROR, exception=exc)
return False
ttWords = ttNovel + ttNotes