diff --git a/CMakeLists.txt b/CMakeLists.txt index 3323c43..2c4d953 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.4.5 + VERSION 0.5.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 786652f..c0b0933 100644 --- a/README.md +++ b/README.md @@ -69,25 +69,21 @@ After you did a `make install`, a project consisting of one file can be compiled ## Error codes +mastodon-cpp will never use error codes below 11, except 0. + | Code | Explanation | | --------: |:------------------------------| | 0 | No error | -| 1 | Invalid call | -| 2 | Not implemented | -| 3 | URL changed (HTTP 301 or 308) | -| 4 | Aborted by user | -| 10 | Failed to connect | -| 11 | Couldn't resolve host | -| 12 | Network is unreachable | +| 11 | Invalid call | +| 12 | Not implemented | +| 13 | URL changed (HTTP 301 or 308) | +| 14 | Aborted by user | +| 20 | Failed to connect | +| 21 | Couldn't resolve host | +| 22 | Network is unreachable | | 100 - 999 | HTTP status codes | | 65535 | Unknown error | -| | Deprecated (since 0.2.10) | -| --------: |:------------------------------| -| 16 | Connection failed | -| 17 | TLS error | -| 18 | Invalid response from server | - If you use a debug build, you get more verbose error messages. ## Useful links diff --git a/src/api_delete.cpp b/src/api_delete.cpp index d6d28b7..3e5169a 100644 --- a/src/api_delete.cpp +++ b/src/api_delete.cpp @@ -36,7 +36,7 @@ const std::uint16_t API::del(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } @@ -71,7 +71,7 @@ const std::uint16_t API::del(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/api_get.cpp b/src/api_get.cpp index f749ac9..cc6906c 100644 --- a/src/api_get.cpp +++ b/src/api_get.cpp @@ -79,7 +79,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } @@ -166,7 +166,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/api_get_stream.cpp b/src/api_get_stream.cpp index 6ccb483..313b52d 100644 --- a/src/api_get_stream.cpp +++ b/src/api_get_stream.cpp @@ -42,7 +42,7 @@ const std::uint16_t API::get_stream(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } @@ -69,7 +69,7 @@ const std::uint16_t API::get_stream(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/api_patch.cpp b/src/api_patch.cpp index 6eb89db..c167061 100644 --- a/src/api_patch.cpp +++ b/src/api_patch.cpp @@ -35,7 +35,7 @@ const std::uint16_t API::patch(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/api_post.cpp b/src/api_post.cpp index cf9af08..3238f1f 100644 --- a/src/api_post.cpp +++ b/src/api_post.cpp @@ -66,7 +66,7 @@ const std::uint16_t API::post(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } @@ -142,7 +142,7 @@ const std::uint16_t API::post(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/api_put.cpp b/src/api_put.cpp index 692430b..01cb1e4 100644 --- a/src/api_put.cpp +++ b/src/api_put.cpp @@ -38,7 +38,7 @@ const std::uint16_t API::put(const Mastodon::API::v1 &call, break; default: ttdebug << "ERROR: Invalid call.\n"; - return 1; + return 11; break; } diff --git a/src/examples/example1_dump_json.cpp b/src/examples/example1_dump_json.cpp index 5454a9d..40d7db4 100644 --- a/src/examples/example1_dump_json.cpp +++ b/src/examples/example1_dump_json.cpp @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) return ret; } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example2_parse_account.cpp b/src/examples/example2_parse_account.cpp index 893ac5f..908a3f3 100644 --- a/src/examples/example2_parse_account.cpp +++ b/src/examples/example2_parse_account.cpp @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) } } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example3_mastocron.cpp b/src/examples/example3_mastocron.cpp index 04073b3..047618a 100644 --- a/src/examples/example3_mastocron.cpp +++ b/src/examples/example3_mastocron.cpp @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) pt::write_json(filename, config); } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example4_update_credentials.cpp b/src/examples/example4_update_credentials.cpp index 10cffa6..5c16fbe 100644 --- a/src/examples/example4_update_credentials.cpp +++ b/src/examples/example4_update_credentials.cpp @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { std::cout << answer << '\n'; } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example5_follow_unfollow.cpp b/src/examples/example5_follow_unfollow.cpp index df6034a..0415e1a 100644 --- a/src/examples/example5_follow_unfollow.cpp +++ b/src/examples/example5_follow_unfollow.cpp @@ -37,7 +37,7 @@ int main(int argc, char *argv[]) { // std::cout << answer << '\n'; } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example6_toot_delete-toot.cpp b/src/examples/example6_toot_delete-toot.cpp index f8bed9a..ef15012 100644 --- a/src/examples/example6_toot_delete-toot.cpp +++ b/src/examples/example6_toot_delete-toot.cpp @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) return ret; } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/examples/example7_register_app.cpp b/src/examples/example7_register_app.cpp index ff40366..8b25311 100644 --- a/src/examples/example7_register_app.cpp +++ b/src/examples/example7_register_app.cpp @@ -72,7 +72,7 @@ int main(int argc, char *argv[]) return ret; } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << url << '\n'; diff --git a/src/examples/example8_rate_limiting.cpp b/src/examples/example8_rate_limiting.cpp index 006c24b..1aeeedb 100644 --- a/src/examples/example8_rate_limiting.cpp +++ b/src/examples/example8_rate_limiting.cpp @@ -37,7 +37,7 @@ int main(int argc, char *argv[]) std::cout << "🔥 Please let the server cool off a bit! 🔥\n"; } } - else if (ret == 3) + else if (ret == 13) { std::cerr << "The URL has permanently changed.\n" << "New URL: " << answer << '\n'; diff --git a/src/http_sync.cpp b/src/http_sync.cpp index 0d7d7a8..eec563d 100644 --- a/src/http_sync.cpp +++ b/src/http_sync.cpp @@ -71,6 +71,7 @@ const std::uint16_t API::http::request_sync(const method &meth, std::list headers; request.setOpt("https://" + _instance + path); + ttdebug << "User-Agent: " << parent.get_useragent() << "\n"; request.setOpt(parent.get_useragent()); headers.push_back("Connection: close"); @@ -127,7 +128,7 @@ const std::uint16_t API::http::request_sync(const method &meth, { // Moved Permanently or Permanent Redirect // return new URL answer = curlpp::infos::EffectiveUrl::get(request); - return 3; + return 13; } else { @@ -140,22 +141,24 @@ const std::uint16_t API::http::request_sync(const method &meth, "Failed writing body", 19) == 0) { ttdebug << "Request was aborted by user\n"; - return 4; + return 14; } else if (std::strncmp(e.what(), "Failed to connect to", 20) == 0) { - ret = 10; + ret = 20; } else if (std::strncmp(e.what(), - "Couldn't resolve host", 21) == 0) + "Couldn't resolve host", 21) == 0 || + std::strncmp(e.what(), + "Could not resolve host", 22) == 0) { - ret = 11; + ret = 21; } else if (std::strncmp(e.what(), "Network is unreachable", 22) == 0) { - ret = 12; + ret = 22; } else { diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 6525a2f..f78a0e8 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -184,7 +184,7 @@ const std::uint16_t API::register_app1(const string &client_name, return 0; } - else if (ret == 3) + else if (ret == 13) { url = answer; return ret; diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index cc1c475..7c4a2f9 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -42,16 +42,17 @@ namespace Mastodon * @brief Class for the Mastodon API. All input is expected to be UTF-8. * Binary data must be base64-encoded or a filename. * @section error Error codes + * mastodon-cpp will never use error codes below 11, except 0. * | Code | Explanation | * | --------: |:------------------------------| * | 0 | No error | - * | 1 | Invalid call | - * | 2 | Not implemented | - * | 3 | URL changed (HTTP 301 or 308) | - * | 4 | Aborted by user | - * | 10 | Failed to connect | - * | 11 | Couldn't resolve host | - * | 12 | Network is unreachable | + * | 11 | Invalid call | + * | 12 | Not implemented | + * | 13 | URL changed (HTTP 301 or 308) | + * | 14 | Aborted by user | + * | 20 | Failed to connect | + * | 21 | Couldn't resolve host | + * | 22 | Network is unreachable | * | 100 - 999 | HTTP status codes | * | 65535 | Unknown exception | */ @@ -91,8 +92,8 @@ public: * @param formdata The form data for PATCH and POST request. * @param answer The answer from the server * - * @return @ref error "Error code". If the URL has permanently changed, 3 - * is returned and answer is set to the new URL. + * @return @ref error "Error code". If the URL has permanently changed, + * 13 is returned and answer is set to the new URL. */ const std::uint16_t request_sync(const method &meth, const std::string &path, @@ -261,7 +262,7 @@ public: * @param client_secret Returned * @param url Returned, used to generate code for register_app2 * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and url is set to the new URL. */ const std::uint16_t register_app1(const std::string &client_name, @@ -332,7 +333,7 @@ public: * @param answer The answer from the server. Usually JSON. On error an * empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get(const Mastodon::API::v1 &call, @@ -348,7 +349,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get(const Mastodon::API::v1 &call, @@ -364,7 +365,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get(const Mastodon::API::v1 &call, @@ -379,7 +380,7 @@ public: * @param answer The answer from the server. Usually JSON. On error an * empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get(const std::string &call, @@ -395,7 +396,7 @@ public: * @param ptr Pointer to the http object. Can be used to call * ptr->abort_stream() * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get_stream(const Mastodon::API::v1 &call, @@ -412,7 +413,7 @@ public: * @param ptr Pointer to the http object. Can be used to call * ptr->abort_stream() * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get_stream(const Mastodon::API::v1 &call, @@ -428,7 +429,7 @@ public: * @param ptr Pointer to the http object. Can be used to call * ptr->abort_stream() * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t get_stream(const std::string &call, @@ -445,7 +446,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t patch(const Mastodon::API::v1 &call, @@ -459,7 +460,7 @@ public: * @param answer The answer from the server. Usually JSON. On error an * empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t post(const Mastodon::API::v1 &call, @@ -473,7 +474,7 @@ public: * @param answer The answer from the server. Usually JSON. On error an * empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t post(const Mastodon::API::v1 &call, @@ -491,7 +492,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t post(const Mastodon::API::v1 &call, @@ -509,7 +510,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t post(const Mastodon::API::v1 &call, @@ -527,7 +528,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t post(const std::string &call, @@ -544,7 +545,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t put(const Mastodon::API::v1 &call, @@ -561,7 +562,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t put(const std::string &call, @@ -613,7 +614,7 @@ public: * @param answer The answer from the server. Usually JSON. On error * an empty string. * - * @return @ref error "Error code". If the URL has permanently changed, 3 + * @return @ref error "Error code". If the URL has permanently changed, 13 * is returned and answer is set to the new URL. */ const std::uint16_t del(const std::string &call,