From 22780c7c414bf6359059ce37106edad6bf60fb58 Mon Sep 17 00:00:00 2001 From: Tobias Deiminger Date: Fri, 25 Mar 2022 08:53:40 +0100 Subject: [PATCH 1/2] Fix HTTP status for actions with empty match set If we requested an API action with a valid JSON filter that matches for zero objects {"type": "Service", "filter": "service.name==\"doesnt-exist\""} the actions API responded with HTTP/1.1 500 Internal Server Error {"results":[]} This is semanticlly incorrect, because the server did exactly and successfully what the user requested, namely to apply an action to zero objects. In practice such empty filter match sets are common if you use wildcards or greater/less comparisons in filters. Therefore return 200 - Ok with an empty result list in such a case. --- lib/remote/actionshandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remote/actionshandler.cpp b/lib/remote/actionshandler.cpp index 80f06e603..f6f094d1e 100644 --- a/lib/remote/actionshandler.cpp +++ b/lib/remote/actionshandler.cpp @@ -123,7 +123,7 @@ bool ActionsHandler::HandleRequest( statusCode = *okStatusCodes.begin(); } else if (nonOkSize == 1u) { statusCode = *nonOkStatusCodes.begin(); - } else if (okSize >= 2u && nonOkSize == 0u) { + } else if ((okSize == 0u || okSize >= 2u) && nonOkSize == 0u) { statusCode = 200; } From 60326987bab4090c409bda557e1b624f7ff57add Mon Sep 17 00:00:00 2001 From: Tobias Deiminger Date: Fri, 25 Mar 2022 11:29:51 +0100 Subject: [PATCH 2/2] Fix API status code for action filter exception If FilterUtility::GetFilterTargets threw an exception, the API reported 404 - Objects not found. That's wrong. If no objects had been found, the function would have returned empty list instead of throwing an exception. By looking at filterutility.cpp we see there are two reasons for exceptions: - throw std::invalid_argument, if the filter expression was invalid - thorw ScriptError, if filter has no permission for object Reflect this with HTTP status code 400 and 401 respectively. --- lib/remote/actionshandler.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/remote/actionshandler.cpp b/lib/remote/actionshandler.cpp index f6f094d1e..69b30fa40 100644 --- a/lib/remote/actionshandler.cpp +++ b/lib/remote/actionshandler.cpp @@ -56,9 +56,14 @@ bool ActionsHandler::HandleRequest( try { objs = FilterUtility::GetFilterTargets(qd, params, user); - } catch (const std::exception& ex) { - HttpUtility::SendJsonError(response, params, 404, - "No objects found.", + } catch (const std::invalid_argument& ex) { + HttpUtility::SendJsonError(response, params, 400, + "Invalid filter.", + DiagnosticInformation(ex)); + return true; + } catch (const ScriptError& ex) { + HttpUtility::SendJsonError(response, params, 401, + "Access denied for filter.", DiagnosticInformation(ex)); return true; }