Move error reporting out of the NWDoc class

This commit is contained in:
Veronica K. B. Olsen
2021-04-25 23:21:51 +02:00
parent 470daf0036
commit 0eb09a2136
5 changed files with 50 additions and 19 deletions
+18 -15
View File
@@ -27,11 +27,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging import logging
import os import os
from functools import partial from nw.enum import nwItemLayout, nwItemClass
from PyQt5.QtCore import QCoreApplication
from nw.enum import nwAlert, nwItemLayout, nwItemClass
from nw.common import isHandle from nw.common import isHandle
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -41,17 +37,13 @@ class NWDoc():
def __init__(self, theProject, theHandle): def __init__(self, theProject, theHandle):
self.theProject = theProject self.theProject = theProject
self.theParent = theProject.theParent
# Internal Variables # Internal Variables
self._docHandle = theHandle self._docHandle = theHandle
self._theItem = self.theProject.projTree[theHandle] self._theItem = self.theProject.projTree[theHandle]
self._fileLoc = None self._fileLoc = None
self._docMeta = {} self._docMeta = {}
self._docError = ""
# Internal Mapping
self.makeAlert = self.theParent.makeAlert
self.tr = partial(QCoreApplication.translate, "NWDoc")
return return
@@ -74,10 +66,13 @@ class NWDoc():
on disk, return an empty string. If something went wrong, return on disk, return an empty string. If something went wrong, return
None. None.
""" """
self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set."
return None return None
if self._theItem is None and not isOrphan: if self._theItem is None and not isOrphan:
self._docError = "Unknown novelWriter document."
return None return None
docFile = self._docHandle+".nwd" docFile = self._docHandle+".nwd"
@@ -105,12 +100,13 @@ class NWDoc():
theText += inFile.read() theText += inFile.read()
except Exception as e: except Exception as e:
self.makeAlert([self.tr("Failed to open document file."), str(e)], nwAlert.ERROR) self._docError = str(e)
# Note: Document must be cleared in case of an io error, # Note: Document must be cleared in case of an io error,
# or else the auto-save or save will try to overwrite it # or else the auto-save or save will try to overwrite it
# with an empty file. Return None to alert the caller. # with an empty file. Return None to alert the caller.
self.clearDocument() self.clearDocument()
return None return None
else: else:
# The document file does not exist, so we assume it's a new # The document file does not exist, so we assume it's a new
# document and initialise an empty text string. # document and initialise an empty text string.
@@ -123,7 +119,9 @@ class NWDoc():
"""Write the document. The file is saved via a temp file in case """Write the document. The file is saved via a temp file in case
of save failure. Returns True if successful, False if not. of save failure. Returns True if successful, False if not.
""" """
self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set."
return False return False
self.theProject.ensureFolderStructure() self.theProject.ensureFolderStructure()
@@ -149,7 +147,7 @@ class NWDoc():
outFile.write(docMeta) outFile.write(docMeta)
outFile.write(docText) outFile.write(docText)
except Exception as e: except Exception as e:
self.makeAlert([self.tr("Could not save document."), str(e)], nwAlert.ERROR) self._docError = str(e)
return False return False
# If we're here, the file was successfully saved, so we can # If we're here, the file was successfully saved, so we can
@@ -164,7 +162,9 @@ class NWDoc():
"""Permanently delete a document source file and related files """Permanently delete a document source file and related files
from the project data folder. from the project data folder.
""" """
self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set."
return False return False
docFile = self._docHandle+".nwd" docFile = self._docHandle+".nwd"
@@ -179,9 +179,7 @@ class NWDoc():
os.unlink(chkFile) os.unlink(chkFile)
logger.debug("Deleted: %s" % chkFile) logger.debug("Deleted: %s" % chkFile)
except Exception as e: except Exception as e:
self.makeAlert( self._docError = str(e)
[self.tr("Could not delete document file."), str(e)], nwAlert.ERROR
)
return False return False
return True return True
@@ -211,6 +209,11 @@ class NWDoc():
return theName, theParent, theClass, theLayout return theName, theParent, theClass, theLayout
def getError(self):
"""Return the last recorded exception.
"""
return self._docError
## ##
# Internal Functions # Internal Functions
## ##
+9 -1
View File
@@ -111,6 +111,11 @@ class GuiDocMerge(QDialog):
for tHandle in finalOrder: for tHandle in finalOrder:
inDoc = NWDoc(self.theProject, tHandle) inDoc = NWDoc(self.theProject, tHandle)
docText = inDoc.readDocument().rstrip("\n") docText = inDoc.readDocument().rstrip("\n")
docErr = inDoc.getError()
if docText is None and docErr:
self.makeAlert(
[self.tr("Failed to open document file."), docErr], nwAlert.ERROR
)
if docText: if docText:
theText += docText+"\n\n" theText += docText+"\n\n"
@@ -132,7 +137,10 @@ class GuiDocMerge(QDialog):
newItem.setStatus(srcItem.itemStatus) newItem.setStatus(srcItem.itemStatus)
outDoc = NWDoc(self.theProject, nHandle) outDoc = NWDoc(self.theProject, nHandle)
outDoc.writeDocument(theText) if not outDoc.writeDocument(theText):
self.theParent.makeAlert(
[self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR
)
self.theParent.treeView.revealNewTreeItem(nHandle) self.theParent.treeView.revealNewTreeItem(nHandle)
self.theParent.openDocument(nHandle, doScroll=True) self.theParent.openDocument(nHandle, doScroll=True)
+11 -1
View File
@@ -129,6 +129,13 @@ class GuiDocSplit(QDialog):
inDoc = NWDoc(self.theProject, self.sourceItem) inDoc = NWDoc(self.theProject, self.sourceItem)
theText = inDoc.readDocument() theText = inDoc.readDocument()
docErr = inDoc.getError()
if theText is None and docErr:
self.theParent.makeAlert(
[self.tr("Failed to open document file."), docErr], nwAlert.ERROR
)
if theText is None: if theText is None:
theText = "" theText = ""
@@ -219,7 +226,10 @@ class GuiDocSplit(QDialog):
theText = theText.rstrip("\n") + "\n\n" theText = theText.rstrip("\n") + "\n\n"
outDoc = NWDoc(self.theProject, nHandle) outDoc = NWDoc(self.theProject, nHandle)
outDoc.writeDocument(theText) if not outDoc.writeDocument(theText):
self.theParent.makeAlert(
[self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR
)
self.theParent.treeView.revealNewTreeItem(nHandle) self.theParent.treeView.revealNewTreeItem(nHandle)
+6 -1
View File
@@ -446,7 +446,12 @@ class GuiDocEditor(QTextEdit):
theItem.setParaCount(self.paraCount) theItem.setParaCount(self.paraCount)
self.saveCursorPosition() self.saveCursorPosition()
self.nwDocument.writeDocument(docText) if not self.nwDocument.writeDocument(docText):
self.theParent.makeAlert([
self.tr("Could not save document."), self.nwDocument.getError()
], nwAlert.ERROR)
return False
self.setDocumentChanged(False) self.setDocumentChanged(False)
self.theIndex.scanText(tHandle, docText) self.theIndex.scanText(tHandle, docText)
+6 -1
View File
@@ -528,7 +528,12 @@ class GuiProjectTree(QTreeWidget):
self.theParent.closeDocument() self.theParent.closeDocument()
delDoc = NWDoc(self.theProject, tHandle) delDoc = NWDoc(self.theProject, tHandle)
delDoc.deleteDocument() if not delDoc.deleteDocument():
self.makeAlert([
self.tr("Could not delete document file."), delDoc.getError()
], nwAlert.ERROR)
return False
self.theIndex.deleteHandle(tHandle) self.theIndex.deleteHandle(tHandle)
self._deleteTreeItem(tHandle) self._deleteTreeItem(tHandle)
self._setTreeChanged(True) self._setTreeChanged(True)