diff --git a/src/lib/jsonParse/jsonRequest.cpp b/src/lib/jsonParse/jsonRequest.cpp index 1ebf8769ab..0679bfbd0e 100644 --- a/src/lib/jsonParse/jsonRequest.cpp +++ b/src/lib/jsonParse/jsonRequest.cpp @@ -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" @@ -66,6 +67,8 @@ /* **************************************************************************** * * jsonRequest - +* +* FIXME PR: review this */ static JsonRequest jsonRequest[] = { @@ -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) @@ -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) diff --git a/src/lib/rest/RestService.cpp b/src/lib/rest/RestService.cpp index 4f055119de..9340b5c814 100644 --- a/src/lib/rest/RestService.cpp +++ b/src/lib/rest/RestService.cpp @@ -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); diff --git a/src/lib/rest/rest.cpp b/src/lib/rest/rest.cpp index 48164c574a..518ce61373 100644 --- a/src/lib/rest/rest.cpp +++ b/src/lib/rest/rest.cpp @@ -984,10 +984,9 @@ 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; } @@ -995,8 +994,8 @@ static int contentTypeCheck(ConnectionInfo* ciP) 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; } @@ -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; } @@ -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); diff --git a/src/lib/rest/restReply.cpp b/src/lib/rest/restReply.cpp index 598f0cc68a..62ad51b121 100644 --- a/src/lib/rest/restReply.cpp +++ b/src/lib/rest/restReply.cpp @@ -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); - } -} diff --git a/src/lib/rest/restReply.h b/src/lib/rest/restReply.h index 3272087472..7b58eb64a9 100644 --- a/src/lib/rest/restReply.h +++ b/src/lib/rest/restReply.h @@ -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 diff --git a/src/lib/serviceRoutines/badRequest.cpp b/src/lib/serviceRoutines/badRequest.cpp index 57ed2c75a3..2928892923 100644 --- a/src/lib/serviceRoutines/badRequest.cpp +++ b/src/lib/serviceRoutines/badRequest.cpp @@ -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(); }