From a7723973dc30dc83fc61bc7553c391ccd53583bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Fri, 17 Nov 2023 13:36:48 +0100 Subject: [PATCH] FIX improve problematic attribute reporting on updates --- src/lib/mongoBackend/MongoCommonUpdate.cpp | 36 ++++++++++++++++------ src/lib/rest/OrionError.cpp | 11 +++++++ src/lib/rest/OrionError.h | 2 ++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/lib/mongoBackend/MongoCommonUpdate.cpp b/src/lib/mongoBackend/MongoCommonUpdate.cpp index afdec2510f..33759a32bd 100644 --- a/src/lib/mongoBackend/MongoCommonUpdate.cpp +++ b/src/lib/mongoBackend/MongoCommonUpdate.cpp @@ -2448,7 +2448,7 @@ static bool updateContextAttributeItem " - offending attribute: " + targetAttr->getName(); cerP->statusCode.fill(SccInvalidParameter, details); - oe->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); + //oe->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); /* Although 'ca' has been already pushed into cerP, the pointer is still valid, of course */ ca->found = false; @@ -2613,7 +2613,7 @@ static bool deleteContextAttributeItem " - attribute not found"; cerP->statusCode.fill(SccInvalidParameter, details); - oe->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); + //oe->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); alarmMgr.badInput(clientIp, "attribute to be deleted is not found", targetAttr->getName()); ca->found = false; @@ -3645,7 +3645,7 @@ static unsigned int updateEntity *attributeAlreadyExistsList += " ]"; } - if ((apiVersion == V2) && (action == ActionTypeUpdate)) + if ((apiVersion == V2) && ((action == ActionTypeUpdate) || (action == ActionTypeDelete))) { for (unsigned int ix = 0; ix < eP->attributeVector.size(); ++ix) { @@ -4451,16 +4451,34 @@ unsigned int processContextElement if (attributeAlreadyExistsError == true) { - std::string details = "one or more of the attributes in the request already exist: " + attributeAlreadyExistsList; - buildGeneralErrorResponse(eP, NULL, responseP, SccBadRequest, details); - responseP->oe.fill(SccInvalidModification, details, ERROR_UNPROCESSABLE); + if (responseP->oe.code == SccInvalidModification) + { + // Another previous entity had problems. Use appendDetails() + responseP->oe.appendDetails(", " + eP->id + " - " + attributeAlreadyExistsList); + } + else + { + // First entity with this problem. Use fill() + std::string details = "one or more of the attributes in the request already exist: " + eP->id + " - " + attributeAlreadyExistsList; + buildGeneralErrorResponse(eP, NULL, responseP, SccBadRequest, details); + responseP->oe.fill(SccInvalidModification, details, ERROR_UNPROCESSABLE); + } } if (attributeNotExistingError == true) { - std::string details = "one or more of the attributes in the request do not exist: " + attributeNotExistingList; - buildGeneralErrorResponse(eP, NULL, responseP, SccBadRequest, details); - responseP->oe.fill(SccInvalidModification, details, ERROR_UNPROCESSABLE); + if (responseP->oe.code == SccInvalidModification) + { + // Another previous entity had problems. Use appendDetails() + responseP->oe.appendDetails(", " + eP->id + " - " + attributeNotExistingList); + } + else + { + // First entity with this problem. Use fill() + std::string details = "one or more of the attributes in the request do not exist: " + eP->id + " - " + attributeNotExistingList; + buildGeneralErrorResponse(eP, NULL, responseP, SccBadRequest, details); + responseP->oe.fill(SccInvalidModification, details, ERROR_UNPROCESSABLE); + } } // Response in responseP diff --git a/src/lib/rest/OrionError.cpp b/src/lib/rest/OrionError.cpp index cbec3a9c9e..9b25f1014d 100644 --- a/src/lib/rest/OrionError.cpp +++ b/src/lib/rest/OrionError.cpp @@ -98,6 +98,17 @@ void OrionError::fill(const StatusCode& sc) +/* **************************************************************************** +* +* OrionError::appendDetails - +*/ +void OrionError::appendDetails(const std::string& _details) +{ + details += _details; +} + + + /* **************************************************************************** * * OrionError::smartRender - diff --git a/src/lib/rest/OrionError.h b/src/lib/rest/OrionError.h index 47833a0dac..318cb83790 100644 --- a/src/lib/rest/OrionError.h +++ b/src/lib/rest/OrionError.h @@ -55,6 +55,8 @@ typedef struct OrionError std::string toJsonV1(void); void fill(HttpStatusCode _code, const std::string& _details, const std::string& _reasonPhrase = ""); void fill(const StatusCode& sc); + void appendDetails(const std::string& _details); + private: void shrinkReasonPhrase(void);