]> granicus.if.org Git - icinga2/commitdiff
Improve error handling for empty packages in /v1/config/packages 6153/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Wed, 7 Mar 2018 12:27:31 +0000 (13:27 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 7 Mar 2018 12:35:09 +0000 (13:35 +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 61c07b5be98cedaffafa10c97d0180ae59fc4b98..e3e54237b91d6603f132056939685c08b4c83fe2 100644 (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;
 
index 55fb8db7885b44b2303caa14d7aa312bddd2bd01..56ed57f7be27a135a4b1982e9c46bfea750fc67b 100644 (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;
 }