From 915c85e9e9188dd9060da60737ac1dd08de8d9a1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Aug 2019 08:00:46 +0200 Subject: [PATCH] Changed error codes and introduced error constants. --- CMakeLists.txt | 2 +- README.adoc | 14 +++++------ src/api/delete.cpp | 2 +- src/api/get.cpp | 6 ++--- src/api/get_stream.cpp | 5 +++- src/api/patch.cpp | 2 +- src/api/post.cpp | 2 +- src/api/put.cpp | 2 +- src/easy/return_types_easy.cpp | 10 ++++++++ src/easy/return_types_easy.hpp | 13 ++++++++++ src/easy/simple_calls.cpp | 8 ++++--- src/http.cpp | 43 ++++++++++------------------------ src/mastodon-cpp.hpp | 15 ++++++------ src/return_types.cpp | 9 +++++++ src/return_types.hpp | 14 +++++++++++ src/types.hpp | 12 ++++++++++ 16 files changed, 102 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fcf9b3..dc6efea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ endif() include(GNUInstallDirs) project (mastodon-cpp - VERSION 0.106.0 + VERSION 0.110.0 LANGUAGES CXX) # DESCRIPTION was introduced in version 3.9. diff --git a/README.adoc b/README.adoc index 66a14e7..fbc5408 100644 --- a/README.adoc +++ b/README.adoc @@ -105,13 +105,13 @@ Not included in this list are entities. |=================================================== | Code | Explanation | 0 | No error -| 22 | Invalid argument -| 78 | URL changed (HTTP 301 or 308) -| 110 | Connection timed out -| 111 | Connection refused (check http_error_code) -| 113 | No route to host / Could not resolve host -| 150 | Encryption error (TODO: CHANGEME!) -| 255 | Unknown error +| 1 | Invalid argument +| 10 | URL changed (HTTP 301 or 308) +| 11 | Connection timed out +| 12 | Connection refused (check http_error_code) +| 13 | No route to host / Could not resolve host +| 14 | Encryption error +| 127 | Unknown error |=================================================== If you use a debug build, you get more verbose error messages. diff --git a/src/api/delete.cpp b/src/api/delete.cpp index 59429a6..a56f212 100644 --- a/src/api/delete.cpp +++ b/src/api/delete.cpp @@ -78,7 +78,7 @@ return_call API::del(const Mastodon::API::v1 &call, default: { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; } } diff --git a/src/api/get.cpp b/src/api/get.cpp index 5c2e5bc..5ce5fed 100644 --- a/src/api/get.cpp +++ b/src/api/get.cpp @@ -192,7 +192,7 @@ const return_call API::get(const Mastodon::API::v1 &call, else { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; } break; } @@ -239,7 +239,7 @@ const return_call API::get(const Mastodon::API::v1 &call, default: { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; } } @@ -276,7 +276,7 @@ const return_call API::get(const Mastodon::API::v2 &call, default: { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; } } diff --git a/src/api/get_stream.cpp b/src/api/get_stream.cpp index c5af438..29e6b3e 100644 --- a/src/api/get_stream.cpp +++ b/src/api/get_stream.cpp @@ -20,6 +20,7 @@ using namespace Mastodon; using std::cerr; +using std::to_string; void API::get_stream(const Mastodon::API::v1 &call, const parameters ¶ms, @@ -57,8 +58,10 @@ void API::get_stream(const Mastodon::API::v1 &call, } default: { + const uint8_t err = static_cast(error::INVALID_ARGUMENT); ttdebug << "ERROR: Invalid call.\n"; - stream = "event: ERROR\ndata: {\"error_code\":22}\n"; + stream = "event: ERROR\ndata: " + "{\"error_code\":" + to_string(err) + "}\n"; return; } } diff --git a/src/api/patch.cpp b/src/api/patch.cpp index 82f6801..576f3ea 100644 --- a/src/api/patch.cpp +++ b/src/api/patch.cpp @@ -32,7 +32,7 @@ return_call API::patch(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; break; } diff --git a/src/api/post.cpp b/src/api/post.cpp index b4a1332..fed3d55 100644 --- a/src/api/post.cpp +++ b/src/api/post.cpp @@ -198,7 +198,7 @@ return_call API::post(const Mastodon::API::v1 &call, default: { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, ""}; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, ""}; } } diff --git a/src/api/put.cpp b/src/api/put.cpp index 7a3c430..7dc3794 100644 --- a/src/api/put.cpp +++ b/src/api/put.cpp @@ -58,7 +58,7 @@ return_call API::put(const Mastodon::API::v1 &call, default: { ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; + return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" }; } } diff --git a/src/easy/return_types_easy.cpp b/src/easy/return_types_easy.cpp index 656b289..8204e36 100644 --- a/src/easy/return_types_easy.cpp +++ b/src/easy/return_types_easy.cpp @@ -51,6 +51,16 @@ Easy::return_entity::return_entity(const uint8_t ec, const string &em, http_error_code = hec; } +template +Easy::return_entity::return_entity(const error ec, const string &em, + const uint16_t hec, const T &ent) + : entity(ent) +{ + error_code = static_cast(ec); + error_message = em; + http_error_code = hec; +} + template Easy::return_entity::return_entity::operator const T() const { diff --git a/src/easy/return_types_easy.hpp b/src/easy/return_types_easy.hpp index 5cc06c8..915a89f 100644 --- a/src/easy/return_types_easy.hpp +++ b/src/easy/return_types_easy.hpp @@ -61,6 +61,19 @@ namespace Easy return_entity(const uint8_t ec, const string &em, const uint16_t hec, const T &ent); + /*! + * @brief Return type for easy Mastodon::Easy::API. + * + * @param ec Error code + * @param em Error message + * @param hec HTTP error code + * @param ent Answer + * + * @since 0.110.0 + */ + return_entity(const error ec, const string &em, + const uint16_t hec, const T &ent); + /*! * @brief Same as return_entity::entity. * diff --git a/src/easy/simple_calls.cpp b/src/easy/simple_calls.cpp index 74dac23..9b1d7e3 100644 --- a/src/easy/simple_calls.cpp +++ b/src/easy/simple_calls.cpp @@ -39,7 +39,8 @@ const return_entity API::send_post(const Status &status) else { ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; - return { 22, "Easy::Status::content can not be empty", 0, Status() }; + return { error::INVALID_ARGUMENT, + "Easy::Status::content can not be empty", 0, Status() }; } if (!status.in_reply_to_id().empty()) @@ -94,8 +95,9 @@ const return_entity API::send_post(const Status &status) else { ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n"; - return { 22, "Easy::Attachment::file can not be empty", - 0, Status() }; + return { error::INVALID_ARGUMENT, + "Easy::Attachment::file can not be empty", 0, + Status() }; } if (!att.description().empty()) { diff --git a/src/http.cpp b/src/http.cpp index 3180f58..4dd2136 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -206,7 +206,7 @@ return_call API::http::request_common(const http_method &meth, { case HTTPResponse::HTTP_OK: { - return { 0, "", http_code, answer }; + return { error::OK, "", http_code, answer }; } // Not using the constants because some are too new for Debian stretch. case 301: // HTTPResponse::HTTP_MOVED_PERMANENTLY @@ -226,8 +226,8 @@ return_call API::http::request_common(const http_method &meth, if (location.substr(pos1, pos2 - pos1) != _instance) { // Return new location if the domain changed. ttdebug << "New location is on another domain.\n"; - return { 78, "Remote address changed", http_code, - location }; + return { error::URL_CHANGED, "Remote address changed", + http_code, location }; } location = location.substr(pos2); @@ -235,7 +235,8 @@ return_call API::http::request_common(const http_method &meth, if (http_code == 301 || http_code == 308) { // Return new location for permanent redirects. - return { 78, "Remote address changed", http_code, location }; + return { error::URL_CHANGED, "Remote address changed", + http_code, location }; } else { @@ -245,7 +246,8 @@ return_call API::http::request_common(const http_method &meth, } default: { - return { 111, "Connection refused", http_code, answer }; + return { error::CONNECTION_REFUSED, "Connection refused", + http_code, answer }; } } } @@ -257,7 +259,7 @@ return_call API::http::request_common(const http_method &meth, } ttdebug << e.displayText() << "\n"; - return { 113, e.displayText(), 0, "" }; + return { error::DNS, e.displayText(), 0, "" }; } catch (const Poco::Net::ConnectionRefusedException &e) { @@ -267,7 +269,7 @@ return_call API::http::request_common(const http_method &meth, } ttdebug << e.displayText() << "\n"; - return { 111, e.displayText(), 0, "" }; + return { error::CONNECTION_REFUSED, e.displayText(), 0, "" }; } catch (const Poco::Net::SSLException &e) { @@ -277,7 +279,7 @@ return_call API::http::request_common(const http_method &meth, } ttdebug << e.displayText() << "\n"; - return { 150, e.displayText(), 0, "" }; + return { error::ENCRYPTION, e.displayText(), 0, "" }; } catch (const Poco::Net::NetException &e) { @@ -287,7 +289,7 @@ return_call API::http::request_common(const http_method &meth, } ttdebug << "Unknown network error: " << e.displayText() << std::endl; - return { 255, e.displayText(), 0, "" }; + return { error::UNKNOWN, e.displayText(), 0, "" }; } catch (const std::exception &e) { @@ -297,35 +299,16 @@ return_call API::http::request_common(const http_method &meth, } ttdebug << "Unknown error: " << e.what() << std::endl; - return { 255, e.what(), 0, "" }; + return { error::UNKNOWN, e.what(), 0, "" }; } } +// FIXME: get_headers() doesn't work anymore. void API::http::get_headers(string &headers) const { headers = _headers; } -size_t API::http::callback_write(char* data, size_t size, size_t nmemb, - string *str) -{ - std::lock_guard lock(_mutex); - str->append(data, size * nmemb); - // ttdebug << "Received " << size * nmemb << " Bytes\n"; - return size * nmemb; -} - -double API::http::callback_progress(double /* dltotal */, double /* dlnow */, - double /* ultotal */, double /* ulnow */) -{ - if (_cancel_stream) - { - // This throws the runtime error: Callback aborted - return 1; - } - return 0; -} - void API::http::cancel_stream() { _cancel_stream = true; diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 7f30d43..dc506c3 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -46,7 +46,6 @@ using Poco::Net::HTMLForm; */ namespace Mastodon { - // TODO: error enum, different error codes. /*! * @brief Interface to the Mastodon API. * @@ -57,13 +56,13 @@ namespace Mastodon * | Code | Explanation | * | --------: |:-------------------------------------------| * | 0 | No error | - * | 22 | Invalid argument | - * | 78 | URL changed (HTTP 301 or 308) | - * | 110 | Connection timed out | - * | 111 | Connection refused (check http_error_code) | - * | 113 | No route to host / Could not resolve host | - * | 150 | Encryption error | - * | 255 | Unknown error | + * | 1 | Invalid argument | + * | 10 | URL changed (HTTP 301 or 308) | + * | 11 | Connection timed out | + * | 12 | Connection refused (check http_error_code) | + * | 13 | No route to host / Could not resolve host | + * | 14 | Encryption error | + * | 127 | Unknown error | * * @since before 0.11.0 */ diff --git a/src/return_types.cpp b/src/return_types.cpp index 90c70dc..06411c6 100644 --- a/src/return_types.cpp +++ b/src/return_types.cpp @@ -57,4 +57,13 @@ namespace Mastodon error_message = em; http_error_code = hec; } + + return_call::return_call(const error ec, const string &em, + const uint16_t hec, const string &a) + : answer(a) + { + error_code = static_cast(ec); + error_message = em; + http_error_code = hec; + } } diff --git a/src/return_types.hpp b/src/return_types.hpp index d5827c0..d86ce54 100644 --- a/src/return_types.hpp +++ b/src/return_types.hpp @@ -19,6 +19,7 @@ #include #include +#include "types.hpp" using std::uint8_t; using std::uint16_t; @@ -113,6 +114,19 @@ namespace Mastodon return_call(const uint8_t ec, const string &em, const uint16_t hec, const string &a); + /*! + * @brief Return type for Mastodon::API. + * + * @param ec Error code + * @param em Error message + * @param hec HTTP error code + * @param a Answer + * + * @since 0.110.0 + */ + return_call(const error ec, const string &em, + const uint16_t hec, const string &a); + /*! * @brief Same es return_call::answer. * diff --git a/src/types.hpp b/src/types.hpp index b523553..1916e7b 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -93,6 +93,18 @@ namespace Mastodon DELETE, GET_STREAM }; + + enum class error + { + OK = 0, + INVALID_ARGUMENT = 1, + URL_CHANGED = 10, + CONNECTION_TIMEOUT = 11, + CONNECTION_REFUSED = 12, + DNS = 13, + ENCRYPTION = 14, + UNKNOWN = 127 + }; } #endif // MASTODON_CPP_TYPES_HPP