diff options
Diffstat (limited to 'service/apiImpl.cpp')
-rw-r--r-- | service/apiImpl.cpp | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/service/apiImpl.cpp b/service/apiImpl.cpp index 588d360..1de31cb 100644 --- a/service/apiImpl.cpp +++ b/service/apiImpl.cpp @@ -1,8 +1,9 @@ #include "apiImpl.h" +#include "uptr.h" #include <sql/getServices.sql.h> #include <buffer.h> -#include <memory> +#include <compileTimeFormatter.h> #include <libxml/xpath.h> #include <libxml/xpathInternals.h> @@ -21,35 +22,42 @@ namespace MirrorSearch { return fetch<SearchServices>(sql::getServices); } - typedef std::shared_ptr<xmlDoc> xmlDocSPtr; - typedef std::shared_ptr<xmlXPathContext> xmlXPathContextSPtr; - typedef std::shared_ptr<xmlXPathObject> xmlXPathObjectSPtr; + template<typename Fmt, typename ... P> + void + libxmlErrorHandler(const std::string & fn, const P & ... p) + { + throw XmlError(Fmt::get(fn, xmlGetLastError()->message, p...)); + } + template<typename Fmt, typename ... P> + auto lEHB(const P & ... p) + { + return std::bind(&libxmlErrorHandler<Fmt, P...>, std::placeholders::_1, p...); + } + typedef UPtr<xmlDoc> xmlDocSPtr; + typedef UPtr<xmlXPathContext> xmlXPathContextSPtr; + typedef UPtr<xmlXPathObject> xmlXPathObjectSPtr; + + AdHocFormatter(Read, "Failed to read in %? (%?) [%?, %?]"); static auto getDoc(const ::std::string & url, int flags) { - if (auto doc = xmlDocSPtr(htmlReadFile(url.c_str(), NULL, flags), xmlFreeDoc)) { - return doc; - } - throw XmlError("Failed to open " + url); + return make_unique(htmlReadFile, xmlFreeDoc, lEHB<Read>(url, flags), url.c_str(), (const char*)NULL, flags); } + AdHocFormatter(XPathCtx, "Failed to create xpath context in %? (%?)"); static auto getXPathCxt(const xmlDocSPtr & doc) { - if (auto xpathCtx = xmlXPathContextSPtr(xmlXPathNewContext(doc.get()), xmlXPathFreeContext)) { - return xpathCtx; - } - throw XmlError("Failed to create xpath context"); + return make_unique(xmlXPathNewContext, xmlXPathFreeContext, lEHB<XPathCtx>(), doc.get()); } + AdHocFormatter(XPathEval, "Failed to evaluate xpath in %? (%?) [%?]"); static auto getXPathObj(const ::std::string & xpath, const xmlXPathContextSPtr & ctx, xmlXPathObjectType type) { - if (auto xpathObj = xmlXPathObjectSPtr(xmlXPathEvalExpression(BAD_CAST xpath.c_str(), ctx.get()), xmlXPathFreeObject)) { - if (xpathObj->type != type) { - throw XmlError("Xpath evaluates to wrong type " + xpath); - } - return xpathObj; + auto xpathObj = make_unique(xmlXPathEvalExpression, xmlXPathFreeObject, lEHB<XPathEval>(xpath), BAD_CAST xpath.c_str(), ctx.get()); + if (xpathObj->type != type) { + throw XmlError("Xpath evaluates to wrong type " + xpath); } - throw XmlError("Failed to evaluate xpath " + xpath); + return xpathObj; } void SearchImpl::callService(const ::std::string & fn, const SearchServicePtr & s, SearchHits & sh) const |