Catch `boost::exception`s

This commit is contained in:
Martchus 2022-03-05 11:59:21 +01:00
parent 9b6be51dd3
commit 9acd10fa4c
2 changed files with 26 additions and 1 deletions

View File

@ -31,6 +31,9 @@
#pragma GCC diagnostic pop
#endif
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/exception.hpp>
#include <fstream>
#include <functional>
#include <iostream>
@ -601,6 +604,21 @@ int main(int argc, const char *argv[])
std::bind(&handleResponse, std::ref(url), std::placeholders::_1, std::placeholders::_2, rawArg.isPresent() ? printRawData : printer,
std::ref(returnCode)),
std::string(), config.userName, config.password, verb, std::nullopt, chunkHandler);
ioContext.run();
#ifndef CPP_UTILITIES_DEBUG_BUILD
try {
#endif
ioContext.run();
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const boost::exception &e) {
cerr << Phrases::ErrorMessage << "Unhandled exception: " << Phrases::End << " " << boost::diagnostic_information(e) << Phrases::EndFlush;
return -3;
} catch (const std::exception &e) {
cerr << Phrases::ErrorMessage << "Unhandled exception: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
return -3;
} catch (...) {
cerr << Phrases::ErrorMessage << "Terminated due to unknown error." << Phrases::EndFlush;
return -4;
}
#endif
return returnCode;
}

View File

@ -25,6 +25,9 @@
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/exception.hpp>
#ifdef PLATFORM_LINUX
#include <pthread.h>
#endif
@ -686,6 +689,10 @@ int ServiceSetup::run()
WebAPI::Server::serve(*this);
cout << Phrases::SuccessMessage << "Web server stopped." << Phrases::EndFlush;
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const boost::exception &e) {
cerr << Phrases::ErrorMessage << "Server terminated due to exception: " << Phrases::End << " " << boost::diagnostic_information(e)
<< Phrases::EndFlush;
return -3;
} catch (const std::exception &e) {
cerr << Phrases::ErrorMessage << "Server terminated due to exception: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
return -3;