From 1d31c2cfbaa3adda4c8a7b4a7d44827cd3b5bd03 Mon Sep 17 00:00:00 2001 From: spo Date: Wed, 22 Jul 2015 14:29:08 +0300 Subject: [PATCH] Error management -- Improve Model_Validator --- src/Model/Model_Validator.cpp | 53 +++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index ea64242f0..62715db6e 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -149,23 +149,37 @@ void Model_ValidatorsFactory::addDefaultValidators(std::list& theFeature) const { const static std::string kDefaultId = "Model_FeatureValidator"; + + ModelAPI_ExecState anExecState = theFeature->data()->execState(); + theFeature->setError("", false); + theFeature->data()->execState(anExecState); + // check feature validators first std::map::const_iterator aFeature = myFeatures.find(theFeature->getKind()); if (aFeature != myFeatures.end()) { AttrValidators::const_iterator aValidator = aFeature->second.begin(); for(; aValidator != aFeature->second.end(); aValidator++) { + const std::string& aValidatorID = aValidator->first; + const std::list& anArguments = aValidator->second; std::map::const_iterator aValFind = - myIDs.find(aValidator->first); + myIDs.find(aValidatorID); if (aValFind == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValidator->first + " was not registered"); + Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered"); continue; } const ModelAPI_FeatureValidator* aFValidator = dynamic_cast(aValFind->second); if (aFValidator) { - if (!aFValidator->isValid(theFeature, aValidator->second)) + std::string anError; + if (!aFValidator->isValid(theFeature, anArguments, anError)) { + if (anError.empty()) + anError = "Unknown error."; + theFeature->setValidationError(aValidatorID, anError); return false; + } else { + theFeature->setValidationError(aValidatorID, ""); + } } } } @@ -176,8 +190,15 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr& const ModelAPI_FeatureValidator* aFValidator = dynamic_cast(aDefaultVal->second); if (aFValidator) { - if (!aFValidator->isValid(theFeature, anEmptyArgList)) + std::string anError; + if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) { + if (anError.empty()) + anError = "Unknown error."; + theFeature->setValidationError(kDefaultId, anError); return false; + } else { + theFeature->setValidationError(kDefaultId, ""); + } } } @@ -193,25 +214,33 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr& std::list aLtAttributes = aData->attributesIDs(kAllTypes); std::list::iterator anAttrIter = aLtAttributes.begin(); for (; anAttrIter != aLtAttributes.end(); anAttrIter++) { + const std::string& anAttributeID = *anAttrIter; std::map::const_iterator anAttr = - aFeatureIter->second.find(*anAttrIter); + aFeatureIter->second.find(anAttributeID); if (anAttr != aFeatureIter->second.end()) { // skip not-case attributres, that really may be invalid (issue 671) - if (!const_cast(this)->isCase(theFeature, anAttr->first)) + if (!const_cast(this)->isCase(theFeature, anAttributeID)) continue; AttrValidators::const_iterator aValIter = anAttr->second.cbegin(); for (; aValIter != anAttr->second.cend(); aValIter++) { - std::map::const_iterator aFound = myIDs.find( - aValIter->first); + const std::string& aValidatorID = aValIter->first; + const std::list& anArguments = aValIter->second; + std::map::const_iterator aFound = myIDs.find(aValidatorID); if (aFound == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered"); + Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered"); } else { const ModelAPI_AttributeValidator* anAttrValidator = dynamic_cast(aFound->second); if (anAttrValidator) { - AttributePtr anAttribute = theFeature->data()->attribute(*anAttrIter); - if (!anAttrValidator->isValid(anAttribute, aValIter->second)) { - return false; + AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID); + std::string anError; + if (!anAttrValidator->isValid(anAttribute, anArguments, anError)) { + if (anError.empty()) + anError = "Unknown error."; + theFeature->setAttributeValidationError(anAttributeID, aValidatorID, anError); + return false; + } else { + theFeature->setAttributeValidationError(anAttributeID, aValidatorID, ""); } } } -- 2.39.2