Changed error codes and introduced error constants.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-08-21 08:00:46 +02:00
parent 5ddea09924
commit 915c85e9e9
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
16 changed files with 102 additions and 57 deletions

View File

@ -9,7 +9,7 @@ endif()
include(GNUInstallDirs) include(GNUInstallDirs)
project (mastodon-cpp project (mastodon-cpp
VERSION 0.106.0 VERSION 0.110.0
LANGUAGES CXX) LANGUAGES CXX)
# DESCRIPTION was introduced in version 3.9. # DESCRIPTION was introduced in version 3.9.

View File

@ -105,13 +105,13 @@ Not included in this list are entities.
|=================================================== |===================================================
| Code | Explanation | Code | Explanation
| 0 | No error | 0 | No error
| 22 | Invalid argument | 1 | Invalid argument
| 78 | URL changed (HTTP 301 or 308) | 10 | URL changed (HTTP 301 or 308)
| 110 | Connection timed out | 11 | Connection timed out
| 111 | Connection refused (check http_error_code) | 12 | Connection refused (check http_error_code)
| 113 | No route to host / Could not resolve host | 13 | No route to host / Could not resolve host
| 150 | Encryption error (TODO: CHANGEME!) | 14 | Encryption error
| 255 | Unknown error | 127 | Unknown error
|=================================================== |===================================================
If you use a debug build, you get more verbose error messages. If you use a debug build, you get more verbose error messages.

View File

@ -78,7 +78,7 @@ return_call API::del(const Mastodon::API::v1 &call,
default: default:
{ {
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, "" }; return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
} }
} }

View File

@ -192,7 +192,7 @@ const return_call API::get(const Mastodon::API::v1 &call,
else else
{ {
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, "" }; return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
} }
break; break;
} }
@ -239,7 +239,7 @@ const return_call API::get(const Mastodon::API::v1 &call,
default: default:
{ {
ttdebug << "ERROR: Invalid argument.\n"; 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: default:
{ {
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, "" }; return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
} }
} }

View File

@ -20,6 +20,7 @@
using namespace Mastodon; using namespace Mastodon;
using std::cerr; using std::cerr;
using std::to_string;
void API::get_stream(const Mastodon::API::v1 &call, void API::get_stream(const Mastodon::API::v1 &call,
const parameters &params, const parameters &params,
@ -57,8 +58,10 @@ void API::get_stream(const Mastodon::API::v1 &call,
} }
default: default:
{ {
const uint8_t err = static_cast<uint8_t>(error::INVALID_ARGUMENT);
ttdebug << "ERROR: Invalid call.\n"; ttdebug << "ERROR: Invalid call.\n";
stream = "event: ERROR\ndata: {\"error_code\":22}\n"; stream = "event: ERROR\ndata: "
"{\"error_code\":" + to_string(err) + "}\n";
return; return;
} }
} }

View File

@ -32,7 +32,7 @@ return_call API::patch(const Mastodon::API::v1 &call,
break; break;
default: default:
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, "" }; return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
break; break;
} }

View File

@ -198,7 +198,7 @@ return_call API::post(const Mastodon::API::v1 &call,
default: default:
{ {
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, ""}; return { error::INVALID_ARGUMENT, "Invalid argument", 0, ""};
} }
} }

View File

@ -58,7 +58,7 @@ return_call API::put(const Mastodon::API::v1 &call,
default: default:
{ {
ttdebug << "ERROR: Invalid argument.\n"; ttdebug << "ERROR: Invalid argument.\n";
return { 22, "Invalid argument", 0, "" }; return { error::INVALID_ARGUMENT, "Invalid argument", 0, "" };
} }
} }

View File

@ -51,6 +51,16 @@ Easy::return_entity<T>::return_entity(const uint8_t ec, const string &em,
http_error_code = hec; http_error_code = hec;
} }
template<typename T>
Easy::return_entity<T>::return_entity(const error ec, const string &em,
const uint16_t hec, const T &ent)
: entity(ent)
{
error_code = static_cast<uint8_t>(ec);
error_message = em;
http_error_code = hec;
}
template<typename T> template<typename T>
Easy::return_entity<T>::return_entity::operator const T() const Easy::return_entity<T>::return_entity::operator const T() const
{ {

View File

@ -61,6 +61,19 @@ namespace Easy
return_entity(const uint8_t ec, const string &em, return_entity(const uint8_t ec, const string &em,
const uint16_t hec, const T &ent); 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. * @brief Same as return_entity::entity.
* *

View File

@ -39,7 +39,8 @@ const return_entity<Status> API::send_post(const Status &status)
else else
{ {
ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; 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()) if (!status.in_reply_to_id().empty())
@ -94,8 +95,9 @@ const return_entity<Status> API::send_post(const Status &status)
else else
{ {
ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n"; ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
return { 22, "Easy::Attachment::file can not be empty", return { error::INVALID_ARGUMENT,
0, Status() }; "Easy::Attachment::file can not be empty", 0,
Status() };
} }
if (!att.description().empty()) if (!att.description().empty())
{ {

View File

@ -206,7 +206,7 @@ return_call API::http::request_common(const http_method &meth,
{ {
case HTTPResponse::HTTP_OK: 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. // Not using the constants because some are too new for Debian stretch.
case 301: // HTTPResponse::HTTP_MOVED_PERMANENTLY 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) if (location.substr(pos1, pos2 - pos1) != _instance)
{ // Return new location if the domain changed. { // Return new location if the domain changed.
ttdebug << "New location is on another domain.\n"; ttdebug << "New location is on another domain.\n";
return { 78, "Remote address changed", http_code, return { error::URL_CHANGED, "Remote address changed",
location }; http_code, location };
} }
location = location.substr(pos2); 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) if (http_code == 301 || http_code == 308)
{ // Return new location for permanent redirects. { // 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 else
{ {
@ -245,7 +246,8 @@ return_call API::http::request_common(const http_method &meth,
} }
default: 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"; ttdebug << e.displayText() << "\n";
return { 113, e.displayText(), 0, "" }; return { error::DNS, e.displayText(), 0, "" };
} }
catch (const Poco::Net::ConnectionRefusedException &e) catch (const Poco::Net::ConnectionRefusedException &e)
{ {
@ -267,7 +269,7 @@ return_call API::http::request_common(const http_method &meth,
} }
ttdebug << e.displayText() << "\n"; ttdebug << e.displayText() << "\n";
return { 111, e.displayText(), 0, "" }; return { error::CONNECTION_REFUSED, e.displayText(), 0, "" };
} }
catch (const Poco::Net::SSLException &e) catch (const Poco::Net::SSLException &e)
{ {
@ -277,7 +279,7 @@ return_call API::http::request_common(const http_method &meth,
} }
ttdebug << e.displayText() << "\n"; ttdebug << e.displayText() << "\n";
return { 150, e.displayText(), 0, "" }; return { error::ENCRYPTION, e.displayText(), 0, "" };
} }
catch (const Poco::Net::NetException &e) 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; ttdebug << "Unknown network error: " << e.displayText() << std::endl;
return { 255, e.displayText(), 0, "" }; return { error::UNKNOWN, e.displayText(), 0, "" };
} }
catch (const std::exception &e) 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; 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 void API::http::get_headers(string &headers) const
{ {
headers = _headers; headers = _headers;
} }
size_t API::http::callback_write(char* data, size_t size, size_t nmemb,
string *str)
{
std::lock_guard<std::mutex> 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() void API::http::cancel_stream()
{ {
_cancel_stream = true; _cancel_stream = true;

View File

@ -46,7 +46,6 @@ using Poco::Net::HTMLForm;
*/ */
namespace Mastodon namespace Mastodon
{ {
// TODO: error enum, different error codes.
/*! /*!
* @brief Interface to the Mastodon API. * @brief Interface to the Mastodon API.
* *
@ -57,13 +56,13 @@ namespace Mastodon
* | Code | Explanation | * | Code | Explanation |
* | --------: |:-------------------------------------------| * | --------: |:-------------------------------------------|
* | 0 | No error | * | 0 | No error |
* | 22 | Invalid argument | * | 1 | Invalid argument |
* | 78 | URL changed (HTTP 301 or 308) | * | 10 | URL changed (HTTP 301 or 308) |
* | 110 | Connection timed out | * | 11 | Connection timed out |
* | 111 | Connection refused (check http_error_code) | * | 12 | Connection refused (check http_error_code) |
* | 113 | No route to host / Could not resolve host | * | 13 | No route to host / Could not resolve host |
* | 150 | Encryption error | * | 14 | Encryption error |
* | 255 | Unknown error | * | 127 | Unknown error |
* *
* @since before 0.11.0 * @since before 0.11.0
*/ */

View File

@ -57,4 +57,13 @@ namespace Mastodon
error_message = em; error_message = em;
http_error_code = hec; 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<uint8_t>(ec);
error_message = em;
http_error_code = hec;
}
} }

View File

@ -19,6 +19,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include "types.hpp"
using std::uint8_t; using std::uint8_t;
using std::uint16_t; using std::uint16_t;
@ -113,6 +114,19 @@ namespace Mastodon
return_call(const uint8_t ec, const string &em, return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a); 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. * @brief Same es return_call::answer.
* *

View File

@ -93,6 +93,18 @@ namespace Mastodon
DELETE, DELETE,
GET_STREAM 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 #endif // MASTODON_CPP_TYPES_HPP