From 1969a9071a488398a1bb906285e1f94b3c5f54bb 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 | 12 +++++++++--- lib/remote/configpackageutility.cpp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/remote/configpackageshandler.cpp b/lib/remote/configpackageshandler.cpp index 0b41cd65b..48a624cf3 100644 --- a/lib/remote/configpackageshandler.cpp +++ b/lib/remote/configpackageshandler.cpp @@ -49,7 +49,14 @@ 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, 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); diff --git a/lib/remote/configpackageutility.cpp b/lib/remote/configpackageutility.cpp index b7f04a18f..8760f9e25 100644 --- a/lib/remote/configpackageutility.cpp +++ b/lib/remote/configpackageutility.cpp @@ -58,9 +58,17 @@ void ConfigPackageUtility::DeletePackage(const String& name) std::vector ConfigPackageUtility::GetPackages(void) { + String packageDir = GetPackageDir(); + std::vector 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; } -- 2.40.0