Fix some exceptions crashing the program

This commit is contained in:
King_DuckZ 2020-02-18 11:52:44 +01:00
parent 7170347969
commit d97cf03a34
2 changed files with 27 additions and 18 deletions

View file

@ -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;
}

View file

@ -19,6 +19,7 @@
#include "xpath.hpp"
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xqilla/exceptions/XQException.hpp>
#include <xqilla/exceptions/XMLParseException.hpp>
#include <sstream>
#include <stdexcept>
@ -54,31 +55,35 @@ namespace duck {
auto XPath::run_query (const std::string& parXML, const std::vector<std::string>& parQueries) -> BatchResults {
XQilla& xqilla = m_xqilla;
XercesConfiguration xconfig;
AutoDelete<DynamicContext> context(xqilla.createContext(XQilla::XQUERY_UPDATE, &xconfig));
xercesc::MemBufInputSource input_buf(reinterpret_cast<const XMLByte*>(parXML.c_str()), parXML.size(), "n/a", false);
Node::Ptr ptr;
BatchResults retval;
try {
ptr = context->parseDocument(input_buf);
AutoDelete<DynamicContext> context(xqilla.createContext(XQilla::XQUERY_UPDATE, &xconfig));
Node::Ptr ptr = context->parseDocument(input_buf);
context->setContextItem(ptr);
for (const auto& xpath : parQueries) {
AutoDelete<XQQuery> query(xqilla.parse(X(xpath.c_str())));
context->setContextPosition(1);
context->setContextSize(1);
Result result = query->execute(context);
Item::Ptr item;
std::vector<std::pair<std::string, std::string>> 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<XQQuery> query(xqilla.parse(X(xpath.c_str())));
context->setContextPosition(1);
context->setContextSize(1);
Result result = query->execute(context);
Item::Ptr item;
std::vector<std::pair<std::string, std::string>> 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;
}