summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/exceptions.hpp78
-rw-r--r--include/mastodonpp.hpp21
-rw-r--r--src/exceptions.cpp50
3 files changed, 146 insertions, 3 deletions
diff --git a/include/exceptions.hpp b/include/exceptions.hpp
new file mode 100644
index 0000000..875ef0f
--- /dev/null
+++ b/include/exceptions.hpp
@@ -0,0 +1,78 @@
+/* This file is part of mastodonpp.
+ * Copyright © 2020 tastytea <tastytea@tastytea.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MASTODONPP_EXCEPTIONS_HPP
+#define MASTODONPP_EXCEPTIONS_HPP
+
+#include <curl/curl.h>
+
+#include <cstdint>
+#include <exception>
+#include <string>
+
+namespace mastodonpp
+{
+
+using std::uint16_t;
+using std::exception;
+using std::string;
+
+/*!
+ * @brief Exception for libcurl errors.
+ *
+ * @since 0.1.0
+ */
+class CURLException : public exception
+{
+public:
+ /*!
+ * @brief Constructor with error code and message.
+ *
+ * @since 0.1.0
+ */
+ explicit CURLException(const CURLcode &error, string message);
+
+ /*!
+ * @brief Constructor with error code, message and error buffer.
+ *
+ * @since 0.1.0
+ */
+ explicit CURLException(const CURLcode &error, string message,
+ string error_buffer);
+
+ /*!
+ * @brief The error code returned by libcurl.
+ *
+ * @since 0.1.0
+ */
+ const CURLcode error_code;
+
+ /*!
+ * @brief Returns the error code, message and error buffer.
+ *
+ * @since 0.1.0
+ */
+ [[nodiscard]]
+ const char *what() const noexcept override;
+
+private:
+ const string _message;
+ const string _error_buffer;
+};
+
+} // namespace mastodonpp
+
+#endif // MASTODONPP_EXCEPTIONS_HPP
diff --git a/include/mastodonpp.hpp b/include/mastodonpp.hpp
index 6ca882d..9d32d9c 100644
--- a/include/mastodonpp.hpp
+++ b/include/mastodonpp.hpp
@@ -18,6 +18,7 @@
#define MASTODONPP_HPP
#include "api.hpp"
+#include "exceptions.hpp"
#include "instance.hpp"
#include "request.hpp"
#include "return_types.hpp"
@@ -42,12 +43,26 @@
*
* Or compile your code with `g++ $(pkg-config --cflags --libs mastodonpp)`.
*
- * @section Example
+ * @subsection Example
*
* @code
- * mastodonpp::Instance instance{"example.com", ""};
- * mastodonpp::Request request{instance};
+ * try
+ * {
+ * mastodonpp::Instance instance{"example.com", ""};
+ * mastodonpp::Request request{instance};
+ * auto answer{request.get(mastodonpp::API::v1::instance)};
+ * std::cout << answer << std::endl;
+ * }
+ * catch (const mastodonpp::CURLException &e)
+ * {
+ * std::cerr << e.what() << std::endl;
+ * }
* @endcode
+ *
+ * @section exceptions Exceptions
+ *
+ * Any unrecoverable libcurl error will be thrown as a
+ * mastodonpp::CURLException.
*/
/*!
diff --git a/src/exceptions.cpp b/src/exceptions.cpp
new file mode 100644
index 0000000..381b6d8
--- /dev/null
+++ b/src/exceptions.cpp
@@ -0,0 +1,50 @@
+/* This file is part of mastodonpp.
+ * Copyright © 2020 tastytea <tastytea@tastytea.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "exceptions.hpp"
+
+#include <utility>
+
+namespace mastodonpp
+{
+
+using std::to_string;
+using std::move;
+
+CURLException::CURLException(const CURLcode &error, string message)
+ : error_code{error}
+ , _message{move(message)}
+{}
+
+CURLException::CURLException(const CURLcode &error, string message,
+ string error_buffer)
+ : error_code{error}
+ , _message{move(message)}
+ , _error_buffer{move(error_buffer)}
+{}
+
+const char *CURLException::what() const noexcept
+{
+ static string error_string{"libCURL error: " + to_string(error_code)
+ + " - " + _message};
+ if (!_error_buffer.empty())
+ {
+ error_string.append("[" + _error_buffer + "]");
+ }
+ return error_string.c_str();
+}
+
+} // namespace mastodonpp