Skip to content

Commit

Permalink
REMOVE restErrorReplyGet() function in favour of OrionError usage
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Aug 8, 2024
1 parent 07113ae commit 839f795
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 103 deletions.
24 changes: 11 additions & 13 deletions src/lib/jsonParse/jsonRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "logMsg/traceLevels.h"

#include "common/limits.h"
#include "common/errorMessages.h"
#include "alarmMgr/alarmMgr.h"
#include "ngsi/Request.h"
#include "ngsi/ParseData.h"
Expand Down Expand Up @@ -66,6 +67,8 @@
/* ****************************************************************************
*
* jsonRequest -
*
* FIXME PR: review this
*/
static JsonRequest jsonRequest[] =
{
Expand Down Expand Up @@ -194,13 +197,13 @@ std::string jsonTreat
std::string errorReply;
char reqTypeV[STRING_SIZE_FOR_INT];

restErrorReplyGet(ciP, SccBadRequest, details, &errorReply);
OrionError oe(SccBadRequest, details);
snprintf(reqTypeV, sizeof(reqTypeV), "%d", request);

std::string extra = std::string("RequestType ") + reqTypeV + " (" + requestType(request) + ")";
alarmMgr.badInput(clientIp, "no request treating object found for RequestType", extra);

return errorReply;
return oe.toJson();
}

if (reqPP != NULL)
Expand All @@ -218,27 +221,22 @@ std::string jsonTreat
}
catch (const std::exception &e)
{
std::string errorReply;
restErrorReplyGet(ciP, SccBadRequest, "JSON Parse Error", &errorReply);
OrionError oe(SccBadRequest, ERROR_DESC_PARSE, ERROR_PARSE);
alarmMgr.badInput(clientIp, "JSON Parse Error", e.what());
return errorReply;
return oe.toJson();
}
catch (...)
{
std::string errorReply;

restErrorReplyGet(ciP, SccBadRequest, "JSON Generic Error", &errorReply);
OrionError oe(SccBadRequest, ERROR_DESC_PARSE, ERROR_PARSE);
alarmMgr.badInput(clientIp, "JSON parse generic error");
return errorReply;
return oe.toJson();
}

if (res != "OK")
{
std::string answer;
alarmMgr.badInput(clientIp, "JSON Parse Error", res);
ciP->httpStatusCode = SccBadRequest;
restErrorReplyGet(ciP, ciP->httpStatusCode, res, &answer);
return answer;
OrionError oe(SccBadRequest, ERROR_DESC_PARSE, ERROR_PARSE);
return oe.toJson();
}

if (ciP->inCompoundValue == true)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/rest/RestService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)
// ... and this here is the error that is returned. A 400 Bad Request with "service XXX not recognized" as payload
//
std::string details = std::string("service '") + ciP->url + "' not recognized";
std::string answer;

restErrorReplyGet(ciP, SccBadRequest, ERROR_DESC_BAD_REQUEST_SERVICE_NOT_FOUND, &answer);
OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_SERVICE_NOT_FOUND, ERROR_BAD_REQUEST);
std::string answer = oe.toJson();
alarmMgr.badInput(clientIp, details);
ciP->httpStatusCode = SccBadRequest;
restReply(ciP, answer);
Expand Down
18 changes: 8 additions & 10 deletions src/lib/rest/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,19 +984,18 @@ static int contentTypeCheck(ConnectionInfo* ciP)
if (ciP->httpHeaders.contentType.empty())
{
std::string details = "Content-Type header not used, default application/octet-stream is not supported";
OrionError oe(SccUnsupportedMediaType, details);
ciP->answer = oe.toJson();
ciP->httpStatusCode = SccUnsupportedMediaType;
restErrorReplyGet(ciP, SccUnsupportedMediaType, details, &ciP->answer);
ciP->httpStatusCode = SccUnsupportedMediaType;

return 1;
}

// Case 3
if ((ciP->httpHeaders.contentType != "application/json") && (ciP->httpHeaders.contentType != "text/plain"))
{
std::string details = std::string("not supported content type: ") + ciP->httpHeaders.contentType;
ciP->httpStatusCode = SccUnsupportedMediaType;
restErrorReplyGet(ciP, SccUnsupportedMediaType, details, &ciP->answer);
OrionError oe(SccUnsupportedMediaType, details);
ciP->answer = oe.toJson();
ciP->httpStatusCode = SccUnsupportedMediaType;
return 1;
}
Expand Down Expand Up @@ -1464,8 +1463,8 @@ static MHD_Result connectionTreat

snprintf(details, sizeof(details), "payload size: %d, max size supported: %llu", ciP->httpHeaders.contentLength, inReqPayloadMaxSize);
alarmMgr.badInput(clientIp, details);
restErrorReplyGet(ciP, SccRequestEntityTooLarge, details, &ciP->answer);

OrionError oe(SccRequestEntityTooLarge, details);
ciP->answer = oe.toJson();
ciP->httpStatusCode = SccRequestEntityTooLarge;
}

Expand Down Expand Up @@ -1559,9 +1558,8 @@ static MHD_Result connectionTreat
(ciP->httpHeaders.contentLength == 0) &&
((strncasecmp(ciP->url.c_str(), "/log/", 5) != 0) && (strncasecmp(ciP->url.c_str(), "/admin/log", 10) != 0)))
{
std::string errorMsg;

restErrorReplyGet(ciP, SccContentLengthRequired, "Zero/No Content-Length in PUT/POST/PATCH request", &errorMsg);
OrionError oe(SccContentLengthRequired, "Zero/No Content-Length in PUT/POST/PATCH request");
std::string errorMsg = oe.toJson();
ciP->httpStatusCode = SccContentLengthRequired;
restReply(ciP, errorMsg);
alarmMgr.badInput(clientIp, errorMsg);
Expand Down
70 changes: 0 additions & 70 deletions src/lib/rest/restReply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,73 +145,3 @@ void restReply(ConnectionInfo* ciP, const std::string& answer)
MHD_destroy_response(response);
}



/* ****************************************************************************
*
* restErrorReplyGet -
*
* This function renders an error reply depending on the type of the request (ciP->restServiceP->request).
*
* The function is called from more than one place, especially from
* restErrorReply(), but also from where the payload type is matched against the request URL.
* Where the payload type is matched against the request URL, the incoming 'request' is a
* request and not a response.
*/
void restErrorReplyGet(ConnectionInfo* ciP, HttpStatusCode code, const std::string& details, std::string* outStringP)
{
StatusCode errorCode(code, details, "errorCode");

ciP->httpStatusCode = SccOk;

if (ciP->restServiceP->request == RegisterContext)
{
RegisterContextResponse rcr("000000000000000000000000", errorCode);
*outStringP = rcr.toJsonV1();
}
else if (ciP->restServiceP->request == DiscoverContextAvailability)
{
DiscoverContextAvailabilityResponse dcar(errorCode);
*outStringP = dcar.toJsonV1();
}
else if (ciP->restServiceP->request == QueryContext)
{
QueryContextResponse qcr(errorCode);
bool asJsonObject = (ciP->uriParam[URI_PARAM_ATTRIBUTE_FORMAT] == "object" && ciP->outMimeType == JSON);
*outStringP = qcr.toJsonV1(asJsonObject);
}
else if (ciP->restServiceP->request == SubscribeContext)
{
SubscribeContextResponse scr(errorCode);
*outStringP = scr.toJsonV1();
}
else if ((ciP->restServiceP->request == UpdateContextSubscription) || (ciP->restServiceP->request == Ngsi10SubscriptionsConvOp))
{
UpdateContextSubscriptionResponse ucsr(errorCode);
*outStringP = ucsr.toJsonV1();
}
else if (ciP->restServiceP->request == UnsubscribeContext)
{
UnsubscribeContextResponse uncr(errorCode);
*outStringP = uncr.toJsonV1();
}
else if (ciP->restServiceP->request == UpdateContext)
{
UpdateContextResponse ucr(errorCode);
bool asJsonObject = (ciP->uriParam[URI_PARAM_ATTRIBUTE_FORMAT] == "object" && ciP->outMimeType == JSON);
*outStringP = ucr.toJsonV1(asJsonObject);
}
else if (ciP->restServiceP->request == NotifyContext)
{
NotifyContextResponse ncr(errorCode);
*outStringP = ncr.toJsonV1();
}
else
{
OrionError oe(errorCode);

LM_T(LmtRest, ("Unknown request type: '%d'", ciP->restServiceP->request));
ciP->httpStatusCode = oe.code;
*outStringP = oe.setStatusCodeAndSmartRender(&ciP->httpStatusCode);
}
}
6 changes: 0 additions & 6 deletions src/lib/rest/restReply.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,4 @@ extern void restReply(ConnectionInfo* ciP, const std::string& answer);



/* ****************************************************************************
*
* restErrorReplyGet -
*/
extern void restErrorReplyGet(ConnectionInfo* ciP, HttpStatusCode code, const std::string& detail, std::string* outStringP);

#endif
5 changes: 3 additions & 2 deletions src/lib/serviceRoutines/badRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ std::string badRequest

alarmMgr.badInput(ciP->ip, details);

restErrorReplyGet(ciP, SccBadRequest, ERROR_DESC_BAD_REQUEST_SERVICE_NOT_FOUND, &answer);
OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_SERVICE_NOT_FOUND);
ciP->httpStatusCode = SccBadRequest;

return answer;
return oe.toJson();
}

0 comments on commit 839f795

Please sign in to comment.