mirror of https://github.com/Icinga/icinga2.git
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:
parent
ddab94feb3
commit
1969a9071a
|
@ -49,7 +49,14 @@ void ConfigPackagesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& req
|
||||||
{
|
{
|
||||||
FilterUtility::CheckPermission(user, "config/query");
|
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();
|
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());
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticMutex());
|
||||||
ConfigPackageUtility::CreatePackage(packageName);
|
ConfigPackageUtility::CreatePackage(packageName);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
HttpUtility::SendJsonError(response, 500, "Could not create package.",
|
HttpUtility::SendJsonError(response, 500, "Could not create package.", "");
|
||||||
HttpUtility::GetLastParameter(params, "verboseErrors") ? DiagnosticInformation(ex) : "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result1->Set("code", 200);
|
result1->Set("code", 200);
|
||||||
|
|
|
@ -58,9 +58,17 @@ void ConfigPackageUtility::DeletePackage(const String& name)
|
||||||
|
|
||||||
std::vector<String> ConfigPackageUtility::GetPackages(void)
|
std::vector<String> ConfigPackageUtility::GetPackages(void)
|
||||||
{
|
{
|
||||||
|
String packageDir = GetPackageDir();
|
||||||
|
|
||||||
std::vector<String> packages;
|
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;
|
return packages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue