Added support for passing exceptions

This commit is contained in:
tastytea 2018-04-10 10:17:30 +02:00
parent f91220c715
commit bbdcf70efd
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 42 additions and 1 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.8.7
VERSION 0.8.8
LANGUAGES CXX
)

View File

@ -18,6 +18,7 @@
#include <functional> // std::bind
#include <list>
#include <cstring> // std::strncmp
#include <exception>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
@ -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;
}

View File

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

View File

@ -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.