From c3a709c2b9f69b56be7f0167e1cc1d231d708e68 Mon Sep 17 00:00:00 2001 From: Adam Capparelli Date: Mon, 22 Jul 2024 08:37:52 -0400 Subject: [PATCH] commands API will return 400 if command fails on device. Signed-off-by: Adam Capparelli --- src/RESTAPI/RESTAPI_RPC.cpp | 13 +++++++++++++ src/framework/RESTAPI_Handler.h | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/RESTAPI/RESTAPI_RPC.cpp b/src/RESTAPI/RESTAPI_RPC.cpp index 05528c13..9c9b99b2 100644 --- a/src/RESTAPI/RESTAPI_RPC.cpp +++ b/src/RESTAPI/RESTAPI_RPC.cpp @@ -26,6 +26,9 @@ namespace OpenWifi::RESTAPI_RPC { Poco::JSON::Object RetObj; Cmd.to_json(RetObj); if (Handler != nullptr) + if (Cmd.ErrorCode){ + return Handler->ReturnObject(RetObj, Poco::Net::HTTPResponse::HTTP_BAD_REQUEST); + } return Handler->ReturnObject(RetObj); return; } @@ -167,6 +170,15 @@ namespace OpenWifi::RESTAPI_RPC { Cmd.AttachType = ""; } + // If the command fails on the device we should show it as failed and not return 200 OK + if (Cmd.ErrorCode) { + Logger.information(fmt::format( + "Command failed with error on device: {} Reason: {}.", + Cmd.ErrorCode, Cmd.ErrorText)); + return SetCommandStatus(Cmd, Request, Response, Handler, + Storage::CommandExecutionType::COMMAND_FAILED, Logger); + } + if (Cmd.ErrorCode == 0 && Cmd.Command == uCentralProtocol::CONFIGURE) { // we need to post a kafka event for this. if (Params.has(uCentralProtocol::CONFIG) && Params.isObject(uCentralProtocol::CONFIG)) { @@ -175,6 +187,7 @@ namespace OpenWifi::RESTAPI_RPC { DeviceConfigurationChangeKafkaEvent KEvent( Utils::SerialNumberToInt(Cmd.SerialNumber), Utils::Now(), Config); + } } diff --git a/src/framework/RESTAPI_Handler.h b/src/framework/RESTAPI_Handler.h index d6f31be7..3cd9aa07 100644 --- a/src/framework/RESTAPI_Handler.h +++ b/src/framework/RESTAPI_Handler.h @@ -552,8 +552,8 @@ namespace OpenWifi { inline bool IsAuthorized(bool &Expired, bool &Contacted, bool SubOnly = false); - inline void ReturnObject(Poco::JSON::Object &Object) { - PrepareResponse(); + inline void ReturnObject(Poco::JSON::Object &Object, Poco::Net::HTTPResponse::HTTPStatus Status = Poco::Net::HTTPResponse::HTTP_OK) { + PrepareResponse(Status); if (Request != nullptr) { // can we compress ??? auto AcceptedEncoding = Request->find("Accept-Encoding");