Improve error handling for empty packages in /v1/config/packages

- If there is no package main directory, assume "empty packages".
- Catch exceptions thrown through GlobRecursive() and present a better http 500
to the user.

The packages directory tree is automatically created with the first
package creation, either from the user, or by the `_api` package.

fixes #6129
This commit is contained in:
Michael Friedrich 2018-03-07 13:27:31 +01:00 committed by Jean Flach
parent ddab94feb3
commit 1969a9071a
2 changed files with 19 additions and 5 deletions

View File

@ -49,7 +49,14 @@ 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, 500, "Could not retrieve packages.");
return;
}
Array::Ptr results = new Array();
@ -91,8 +98,7 @@ void ConfigPackagesHandler::HandlePost(const ApiUser::Ptr& user, HttpRequest& re
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticMutex());
ConfigPackageUtility::CreatePackage(packageName);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, 500, "Could not create package.",
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
HttpUtility::SendJsonError(response, 500, "Could not create package.", "");
}
result1->Set("code", 200);

View File

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