Skip to content

Commit

Permalink
Merge pull request #366 from Telecominfraproject/WIFI-13985-fix-retur…
Browse files Browse the repository at this point in the history
…n-400-on-error

WIFI-13985: commands API will return 400 if command fails on device.
  • Loading branch information
i-chvets authored Jul 26, 2024
2 parents 5d89107 + c3a709c commit b59d1cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/RESTAPI/RESTAPI_RPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -175,6 +187,7 @@ namespace OpenWifi::RESTAPI_RPC {
DeviceConfigurationChangeKafkaEvent KEvent(
Utils::SerialNumberToInt(Cmd.SerialNumber), Utils::Now(),
Config);

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/framework/RESTAPI_Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit b59d1cb

Please sign in to comment.