From d97cf03a3432cb02f5dfe13fb45b8daa4bdd3779 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 18 Feb 2020 11:52:44 +0100 Subject: [PATCH] Fix some exceptions crashing the program --- src/main.cpp | 4 ++++ src/xpath.cpp | 41 +++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 97c8050..e9ee67e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,10 @@ int main (int argc, char* argv[]) { std::cerr << err.what() << std::endl; return 1; } + catch (const std::runtime_error& err) { + std::cerr << err.what() << std::endl; + return 1; + } return 0; } diff --git a/src/xpath.cpp b/src/xpath.cpp index 75d90a4..40ab734 100644 --- a/src/xpath.cpp +++ b/src/xpath.cpp @@ -19,6 +19,7 @@ #include "xpath.hpp" #include #include +#include #include #include #include @@ -54,31 +55,35 @@ namespace duck { auto XPath::run_query (const std::string& parXML, const std::vector& parQueries) -> BatchResults { XQilla& xqilla = m_xqilla; XercesConfiguration xconfig; - AutoDelete context(xqilla.createContext(XQilla::XQUERY_UPDATE, &xconfig)); xercesc::MemBufInputSource input_buf(reinterpret_cast(parXML.c_str()), parXML.size(), "n/a", false); - Node::Ptr ptr; + BatchResults retval; try { - ptr = context->parseDocument(input_buf); + AutoDelete context(xqilla.createContext(XQilla::XQUERY_UPDATE, &xconfig)); + Node::Ptr ptr = context->parseDocument(input_buf); + context->setContextItem(ptr); + + for (const auto& xpath : parQueries) { + AutoDelete query(xqilla.parse(X(xpath.c_str()))); + context->setContextPosition(1); + context->setContextSize(1); + + Result result = query->execute(context); + Item::Ptr item; + std::vector> new_lst; + while(nullptr != (item = result->next(context))) { + new_lst.push_back(std::make_pair(std::string(), UTF8(item->asString(context)))); + } + + retval.push_back(std::move(new_lst)); + } } catch (const XMLParseException& err) { throw ParseError(err.getXQueryLine(), err.getXQueryColumn(), xercesc::XMLString::transcode(err.getError())); } - context->setContextItem(ptr); - - BatchResults retval; - for (const auto& xpath : parQueries) { - AutoDelete query(xqilla.parse(X(xpath.c_str()))); - context->setContextPosition(1); - context->setContextSize(1); - - Result result = query->execute(context); - Item::Ptr item; - std::vector> new_lst; - while(nullptr != (item = result->next(context))) { - new_lst.push_back(std::make_pair(std::string(), UTF8(item->asString(context)))); - } - retval.push_back(std::move(new_lst)); + catch (const XQException& err) { + throw std::runtime_error(xercesc::XMLString::transcode(err.getError())); } + return retval; }