Merge pull request #6153 from Icinga/fix/api-no-packages

Improve error handling for empty packages in /v1/config/packages
This commit is contained in:
Gunnar Beutner 2018-03-13 09:23:01 +01:00 committed by GitHub
commit 9b0fccfd80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -48,7 +48,15 @@ void ConfigPackagesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& req
{
FilterUtility::CheckPermission(user, "config/query");
std::vector<String> packages = ConfigPackageUtility::GetPackages();
std::vector<String> packages;
try {
packages = ConfigPackageUtility::GetPackages();
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, params, 500, "Could not retrieve packages.",
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
return;
}
ArrayData results;

View File

@ -57,9 +57,17 @@ void ConfigPackageUtility::DeletePackage(const String& name)
std::vector<String> ConfigPackageUtility::GetPackages()
{
String packageDir = GetPackageDir();
std::vector<String> packages;
Utility::Glob(GetPackageDir() + "/*", std::bind(&ConfigPackageUtility::CollectDirNames,
/* Package directory does not exist, no packages have been created thus far. */
if (!Utility::PathExists(packageDir))
return packages;
Utility::Glob(packageDir + "/*", std::bind(&ConfigPackageUtility::CollectDirNames,
_1, std::ref(packages)), GlobDirectory);
return packages;
}