From 429f518b4983d0fbe518c1d2ceaacfc988e7c15d Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 7 Mar 2018 13:27:31 +0100 Subject: [PATCH] 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 --- lib/remote/configpackageshandler.cpp | 10 +++++++++- lib/remote/configpackageutility.cpp | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/remote/configpackageshandler.cpp b/lib/remote/configpackageshandler.cpp index 61c07b5be..e3e54237b 100644 --- a/lib/remote/configpackageshandler.cpp +++ b/lib/remote/configpackageshandler.cpp @@ -48,7 +48,15 @@ void ConfigPackagesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& req { FilterUtility::CheckPermission(user, "config/query"); - std::vector packages = ConfigPackageUtility::GetPackages(); + std::vector 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; diff --git a/lib/remote/configpackageutility.cpp b/lib/remote/configpackageutility.cpp index 55fb8db78..56ed57f7b 100644 --- a/lib/remote/configpackageutility.cpp +++ b/lib/remote/configpackageutility.cpp @@ -57,9 +57,17 @@ void ConfigPackageUtility::DeletePackage(const String& name) std::vector ConfigPackageUtility::GetPackages() { + String packageDir = GetPackageDir(); + std::vector 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; } -- 2.40.0