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
parent 3043a190ca
commit 429f518b49
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;
}