From bbdcf70efd289cead2195a32284b92f78c8ba64c Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 10 Apr 2018 10:17:30 +0200 Subject: [PATCH] Added support for passing exceptions --- CMakeLists.txt | 2 +- src/http.cpp | 11 +++++++++++ src/mastodon-cpp.cpp | 12 ++++++++++++ src/mastodon-cpp.hpp | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cf26d0..956827c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.8.7 + VERSION 0.8.8 LANGUAGES CXX ) diff --git a/src/http.cpp b/src/http.cpp index 1d276d5..8ca9539 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -18,6 +18,7 @@ #include // std::bind #include #include // std::strncmp +#include #include #include #include @@ -136,6 +137,11 @@ const uint_fast16_t API::http::request(const method &meth, } catch (curlpp::RuntimeError &e) { + if (parent.exceptions()) + { + std::rethrow_exception(std::current_exception()); + } + // FIXME: There has to be a better way if (std::strncmp(e.what(), "Failed writing body", 19) == 0) @@ -186,6 +192,11 @@ const uint_fast16_t API::http::request(const method &meth, } catch (curlpp::LogicError &e) { + if (parent.exceptions()) + { + std::rethrow_exception(std::current_exception()); + } + cerr << "LOGIC ERROR: " << e.what() << std::endl; return 0xffff; } diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 74b61e4..3828e1e 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -29,6 +29,7 @@ API::API(const string &instance, const string &access_token) , _access_token(access_token) , _useragent(string("mastodon-cpp/") + global::version) , _http(*this, instance, access_token) +, _exceptions(false) { // } @@ -255,3 +256,14 @@ const string API::get_header(const std::string &header) const return ""; } + +bool API::exceptions(const bool &value) +{ + _exceptions = value; + return _exceptions; +} + +const bool API::exceptions() const +{ + return _exceptions; +} diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index d5a95f5..654cd2b 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -351,6 +351,23 @@ public: */ const string get_header(const string &header) const; + /*! + * @brief Turn exceptions on or off. Defaults to off. + * + * This applies to exceptions from curlpp. curlpp::RuntimeError and + * curlpp::LogicError. + * + * @param value true for on, false for off + * + * @return true if exceptions are turned on, false otherwise + */ + bool exceptions(const bool &value); + + /*! + * @brief Returns true if exceptions are turned on, false otherwise + */ + const bool exceptions() const; + /*! * @brief Make a GET request which doesn't require parameters. * @@ -662,6 +679,7 @@ private: string _access_token; string _useragent; http _http; + bool _exceptions; /*! * @brief Converts map of parameters into a string.