]> granicus.if.org Git - icinga2/commitdiff
Improve error handling for empty packages in /v1/config/packages
authorMichael Friedrich <michael.friedrich@icinga.com>
Wed, 7 Mar 2018 12:27:31 +0000 (13:27 +0100)
committerJean Flach <jean-marcel.flach@icinga.com>
Tue, 13 Mar 2018 10:03:02 +0000 (11:03 +0100)
- 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

lib/remote/configpackageshandler.cpp
lib/remote/configpackageutility.cpp

index 0b41cd65b0d2ce016c1afd1d638c30bcfce01773..48a624cf3e9d7bb2c96761f28046abb2e804c592 100644 (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);
index b7f04a18fca2ec81e877fdc28b6b265836ea09da..8760f9e25c3830af9d6b7213ead148a278065249 100644 (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;
 }