From ddb20468ad438f05c77b02be9b048ace92e8ad6d Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 19 Nov 2018 19:07:56 +0100 Subject: [PATCH] some additions to pR 1794: add a special XMLAttributeError class to indicate an error when accessing a missing attribute in PropertyContainer::Restore make error handling more flexible --- src/App/Application.cpp | 1 + src/App/Document.cpp | 4 ++-- src/App/PropertyContainer.cpp | 18 ++++++++++------- src/Base/Exception.cpp | 37 +++++++++++++++++++++++++++++------ src/Base/Exception.h | 24 ++++++++++++++++++++++- src/Base/Reader.cpp | 4 ++-- src/Mod/Part/App/Geometry.cpp | 2 +- 7 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 1f5e86c1ff..4f282e5fa3 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -1448,6 +1448,7 @@ void Application::initTypes(void) new ExceptionProducer; new ExceptionProducer; new ExceptionProducer; + new ExceptionProducer; new ExceptionProducer; new ExceptionProducer; new ExceptionProducer; diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 05f473b626..67d9490c5f 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -1581,7 +1581,7 @@ Document::readObjects(Base::XMLReader& reader) pObj->setStatus(ObjectStatus::Restore, false); - if(reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInDocumentObject)) { + if (reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInDocumentObject)) { Base::Console().Error("Object \"%s\" was subject to a partial restore. As a result geometry may have changed or be incomplete.\n",name.c_str()); reader.clearPartialRestoreDocumentObject(); } @@ -1882,7 +1882,7 @@ void Document::restore (void) GetApplication().signalFinishRestoreDocument(*this); setStatus(Document::Restoring, false); - if(reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestore)) { + if (reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestore)) { setStatus(Document::PartialRestore, true); Base::Console().Error("There were errors while loading the file. Some data might have been modified or not recovered at all. Look above for more specific information about the objects involved.\n"); } diff --git a/src/App/PropertyContainer.cpp b/src/App/PropertyContainer.cpp index 70ec3fcaa2..ed9caf69c9 100644 --- a/src/App/PropertyContainer.cpp +++ b/src/App/PropertyContainer.cpp @@ -276,13 +276,7 @@ void PropertyContainer::Restore(Base::XMLReader &reader) try { // name and type match if (prop && strcmp(prop->getTypeId().getName(), TypeName.c_str()) == 0) { - prop->Restore(reader); - - if(reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInProperty)) { - Base::Console().Error("Property %s of type %s was subject to a partial restore.\n",PropName.c_str(),TypeName.c_str()); - - reader.clearPartialRestoreProperty(); - } + prop->Restore(reader); } // name matches but not the type else if (prop) { @@ -293,10 +287,20 @@ void PropertyContainer::Restore(Base::XMLReader &reader) else { handleChangedPropertyName(reader, TypeName.c_str(), PropName.c_str()); } + + if (reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInProperty)) { + Base::Console().Error("Property %s of type %s was subject to a partial restore.\n",PropName.c_str(),TypeName.c_str()); + reader.clearPartialRestoreProperty(); + } } catch (const Base::XMLParseException&) { throw; // re-throw } + catch (const Base::RestoreError &) { + reader.setPartialRestore(true); + reader.clearPartialRestoreProperty(); + Base::Console().Error("Property %s of type %s was subject to a partial restore.\n",PropName.c_str(),TypeName.c_str()); + } catch (const Base::Exception &e) { Base::Console().Error("%s\n", e.what()); } diff --git a/src/Base/Exception.cpp b/src/Base/Exception.cpp index cef0c930eb..26a665d30e 100644 --- a/src/Base/Exception.cpp +++ b/src/Base/Exception.cpp @@ -210,14 +210,13 @@ XMLBaseException::XMLBaseException(const XMLBaseException &inst) // --------------------------------------------------------- - XMLParseException::XMLParseException(const char * sMessage) - : Exception(sMessage) + : XMLBaseException(sMessage) { } XMLParseException::XMLParseException(const std::string& sMessage) - : Exception(sMessage) + : XMLBaseException(sMessage) { } @@ -227,13 +226,40 @@ XMLParseException::XMLParseException() } XMLParseException::XMLParseException(const XMLParseException &inst) - : Exception(inst) + : XMLBaseException(inst) { } const char* XMLParseException::what() const throw() { - return Exception::what(); + return XMLBaseException::what(); +} + +// --------------------------------------------------------- + +XMLAttributeError::XMLAttributeError(const char * sMessage) + : XMLBaseException(sMessage) +{ +} + +XMLAttributeError::XMLAttributeError(const std::string& sMessage) + : XMLBaseException(sMessage) +{ +} + +XMLAttributeError::XMLAttributeError() +{ + _sErrMsg = "XML attribute error"; +} + +XMLAttributeError::XMLAttributeError(const XMLAttributeError &inst) + : XMLBaseException(inst) +{ +} + +const char* XMLAttributeError::what() const throw() +{ + return XMLBaseException::what(); } // --------------------------------------------------------- @@ -827,7 +853,6 @@ CADKernelError::CADKernelError(const CADKernelError &inst) } -// --------------------------------------------------------- // --------------------------------------------------------- RestoreError::RestoreError() diff --git a/src/Base/Exception.h b/src/Base/Exception.h index 31abaebf9f..e4b309e580 100644 --- a/src/Base/Exception.h +++ b/src/Base/Exception.h @@ -179,7 +179,7 @@ public: * The XMLParseException is thrown if parsing an XML failed. * @author Werner Mayer */ -class BaseExport XMLParseException : public Exception +class BaseExport XMLParseException : public XMLBaseException { public: /// Construction @@ -197,6 +197,28 @@ public: virtual const char* what() const throw(); }; +/** + * The XMLAttributeError is thrown if a requested attribute doesn't exist. + * @author Werner Mayer + */ +class BaseExport XMLAttributeError : public XMLBaseException +{ +public: + /// Construction + XMLAttributeError(const char * sMessage); + /// Construction + XMLAttributeError(const std::string& sMessage); + /// Construction + XMLAttributeError(); + /// Construction + XMLAttributeError(const XMLAttributeError &inst); + + /// Destruction + virtual ~XMLAttributeError() throw() {} + /// Description of the exception + virtual const char* what() const throw(); +}; + /** File exception handling class * This class is specialized to go with exception thrown in case of File IO Problems. * @author Juergen Riegel diff --git a/src/Base/Reader.cpp b/src/Base/Reader.cpp index 063cf64296..f6b4872f79 100644 --- a/src/Base/Reader.cpp +++ b/src/Base/Reader.cpp @@ -175,8 +175,8 @@ const char* Base::XMLReader::getAttribute (const char* AttrName) const else { // wrong name, use hasAttribute if not sure! std::ostringstream msg; - msg << "Attribute: \"" << AttrName << "\" not found"; - THROWM(Base::AttributeError, msg.str()); + msg << "XML Attribute: \"" << AttrName << "\" not found"; + throw Base::XMLAttributeError(msg.str()); } } diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 7d79678736..545a7f387a 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -3729,7 +3729,7 @@ void GeomLineSegment::Restore (Base::XMLReader &reader) try { setPoints(start, end); } - catch(Base::ValueError &e) { + catch(Base::ValueError&) { // for a line segment construction, the only possibility of a value error is that // the points are too close. The best try to restore is incrementing the distance. // for other objects, the best effort may be just to leave default values.