Report errors with get_stream() in-stream.
the build was successful
Details
the build was successful
Details
This commit is contained in:
parent
a551516be2
commit
09ecd8e5e4
|
@ -83,6 +83,7 @@ g++ -std=c++14 -lmastodon-cpp example.cpp
|
|||
| 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 |
|
||||
| 192 | curlpp runtime error |
|
||||
| 193 | curlpp logic error |
|
||||
| 255 | Unknown error |
|
||||
|
|
|
@ -40,10 +40,8 @@ int main(int argc, char *argv[])
|
|||
// This variable is filled with the stream data.
|
||||
string stream;
|
||||
|
||||
// Get the public timeline, the pointer is set here. The error detection is
|
||||
// not very reliable at the moment, don't count on it.
|
||||
uint8_t ret = masto.get_stream(API::v1::streaming_public, ptr, stream);
|
||||
cout << "Return code: " << std::to_string(ret) << endl;
|
||||
// Get the public timeline. The pointer is set here.
|
||||
masto.get_stream(API::v1::streaming_public, ptr, stream);
|
||||
|
||||
// Listen to the stream for 120 seconds.
|
||||
for (uint8_t counter = 0; counter < 120; ++counter)
|
||||
|
@ -80,12 +78,19 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
case Easy::event_type::Delete:
|
||||
{
|
||||
cout << "Deleted: " << event.data << '\n';
|
||||
cout << "Deleted: " << event.data << endl;
|
||||
break;
|
||||
}
|
||||
case Easy::event_type::Error:
|
||||
{
|
||||
cerr << "Error: " << event.data << endl;
|
||||
ptr->cancel_stream();
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
{
|
||||
cout << "Something undefined happened. 😱\n";
|
||||
cout << event.data << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
using namespace Mastodon;
|
||||
using std::cerr;
|
||||
|
||||
uint8_t API::get_stream(const Mastodon::API::v1 &call,
|
||||
void API::get_stream(const Mastodon::API::v1 &call,
|
||||
const parametermap ¶meters,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream)
|
||||
|
@ -47,7 +47,8 @@ uint8_t API::get_stream(const Mastodon::API::v1 &call,
|
|||
break;
|
||||
default:
|
||||
ttdebug << "ERROR: Invalid call.\n";
|
||||
return 22;
|
||||
stream = "event: ERROR\ndata: {\"error_code\":22}\n";
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -59,15 +60,15 @@ uint8_t API::get_stream(const Mastodon::API::v1 &call,
|
|||
return get_stream(strcall, ptr, stream);
|
||||
}
|
||||
|
||||
uint8_t API::get_stream(const Mastodon::API::v1 &call,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream)
|
||||
void API::get_stream(const Mastodon::API::v1 &call,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream)
|
||||
{
|
||||
return get_stream(call, {}, ptr, stream);
|
||||
}
|
||||
|
||||
uint8_t API::get_stream(const std::string &call, std::unique_ptr<http> &ptr,
|
||||
string &stream)
|
||||
void API::get_stream(const std::string &call, std::unique_ptr<http> &ptr,
|
||||
string &stream)
|
||||
{
|
||||
ptr = std::make_unique<http>(*this, _instance, _access_token);
|
||||
return ptr->request_stream(call, stream);
|
||||
|
|
|
@ -53,7 +53,7 @@ const vector<Easy::stream_event> Easy::parse_stream(
|
|||
const std::string &streamdata)
|
||||
{
|
||||
string stream = streamdata;
|
||||
std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
|
||||
std::regex reevent("event: (update|notification|delete|ERROR)\ndata: (.*)\n");
|
||||
std::smatch match;
|
||||
std::vector<stream_event> vec = {};
|
||||
|
||||
|
@ -69,6 +69,8 @@ const vector<Easy::stream_event> Easy::parse_stream(
|
|||
type = event_type::Notification;
|
||||
else if (event.compare("delete") == 0)
|
||||
type = event_type::Delete;
|
||||
else if (event.compare("ERROR") == 0)
|
||||
type = event_type::Error;
|
||||
|
||||
vec.push_back({ type, data });
|
||||
stream = match.suffix().str();
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace Mastodon
|
|||
/*!
|
||||
* @brief Child of Mastodon::API with abstract methods.
|
||||
*
|
||||
* @since before 0.11.0
|
||||
* @since before 0.100.0
|
||||
*/
|
||||
namespace Easy
|
||||
{
|
||||
|
@ -143,14 +143,14 @@ namespace Easy
|
|||
* @param instance The hostname of your instance
|
||||
* @param access_token The access token
|
||||
*
|
||||
* @since before 0.11.0
|
||||
* @since 0.100.0
|
||||
*/
|
||||
explicit API(const string &instance, const string &access_token);
|
||||
|
||||
/*!
|
||||
* @brief Gets the links from the last answer
|
||||
*
|
||||
* @since before 0.11.0
|
||||
* @since 0.100.0
|
||||
*/
|
||||
const Link get_link() const;
|
||||
|
||||
|
@ -162,14 +162,14 @@ namespace Easy
|
|||
*
|
||||
* @return The new Easy::Status
|
||||
*
|
||||
* @since 0.18.1
|
||||
* @since 0.100.0
|
||||
*/
|
||||
const return_entity<Easy::Status> send_post(const Status &status);
|
||||
|
||||
/*!
|
||||
* @brief Alias for send_post()
|
||||
*
|
||||
* @since 0.17.0
|
||||
* @since 0.100.0
|
||||
*/
|
||||
const return_entity<Easy::Status> send_toot(const Status &status);
|
||||
|
||||
|
@ -183,7 +183,7 @@ namespace Easy
|
|||
*
|
||||
* @return vector of Easy::Notification.
|
||||
*
|
||||
* @since 0.21.0
|
||||
* @since 0.100.0
|
||||
*/
|
||||
const return_entity_vector<Easy::Notification> get_notifications(
|
||||
const uint16_t limit = 20, const string since_id = "",
|
||||
|
|
|
@ -39,6 +39,7 @@ namespace Easy
|
|||
Update,
|
||||
Notification,
|
||||
Delete,
|
||||
Error,
|
||||
Undefined
|
||||
};
|
||||
|
||||
|
|
28
src/http.cpp
28
src/http.cpp
|
@ -57,7 +57,7 @@ return_call API::http::request(const http_method &meth, const string &path,
|
|||
return request_common(meth, path, formdata, answer);
|
||||
}
|
||||
|
||||
uint8_t API::http::request_stream(const string &path, string &stream)
|
||||
void API::http::request_stream(const string &path, string &stream)
|
||||
{
|
||||
static return_call ret;
|
||||
_streamthread = std::thread(
|
||||
|
@ -65,19 +65,14 @@ uint8_t API::http::request_stream(const string &path, string &stream)
|
|||
{
|
||||
ret = request_common(http_method::GET_STREAM, path,
|
||||
curlpp::Forms(), stream);
|
||||
if (!ret)
|
||||
{
|
||||
// Embed the HTTP status code in stream on error.
|
||||
stream += "event: ERROR\ndata: {\"error_code\":"
|
||||
+ std::to_string(ret.error_code) + ",\"http_error\":"
|
||||
+ std::to_string(ret.http_error_code) + "}\n";
|
||||
}
|
||||
});
|
||||
|
||||
// FIXME: Build reliable error detection.
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
if (!ret)
|
||||
{
|
||||
cancel_stream();
|
||||
return ret.error_code;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return_call API::http::request_common(const http_method &meth,
|
||||
|
@ -197,9 +192,14 @@ return_call API::http::request_common(const http_method &meth,
|
|||
}
|
||||
else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0)
|
||||
{
|
||||
ttdebug << "Timeout\n";
|
||||
ttdebug << what << "\n";
|
||||
return { 110, "Connection timed out", 0, "" };
|
||||
}
|
||||
else if (what.compare(0, 23, "Could not resolve host:") == 0)
|
||||
{
|
||||
ttdebug << what << "\n";
|
||||
return { 113, "Could not resolve host", 0, "" };
|
||||
}
|
||||
|
||||
if (parent.exceptions())
|
||||
{
|
||||
|
|
|
@ -62,6 +62,7 @@ namespace Mastodon
|
|||
* | 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 |
|
||||
* | 192 | curlpp runtime error |
|
||||
* | 193 | curlpp logic error |
|
||||
* | 255 | Unknown error |
|
||||
|
@ -117,7 +118,7 @@ namespace Mastodon
|
|||
*
|
||||
* @since 0.100.0
|
||||
*/
|
||||
uint8_t request_stream(const string &path, string &stream);
|
||||
void request_stream(const string &path, string &stream);
|
||||
|
||||
/*!
|
||||
* @brief Get all headers in a string
|
||||
|
@ -538,14 +539,20 @@ namespace Mastodon
|
|||
* @param ptr Pointer to the http object. Can be used to call
|
||||
* ptr->cancel_stream()
|
||||
*
|
||||
* @return @ref error "Error code".
|
||||
* @return @ref error "Errors" are reported in this format:
|
||||
* `{"error_code":uint8_t,"http_error":uint16_t}`. `http_error` is
|
||||
* optional.
|
||||
*
|
||||
* @return @ref error "Errors" are reported in this format:
|
||||
* `{"error_code":uint8_t,"http_error":uint16_t}`. `http_error` is
|
||||
* optional.
|
||||
*
|
||||
* @since 0.100.0
|
||||
*/
|
||||
uint8_t get_stream(const Mastodon::API::v1 &call,
|
||||
const parametermap ¶meters,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream);
|
||||
void get_stream(const Mastodon::API::v1 &call,
|
||||
const parametermap ¶meters,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream);
|
||||
|
||||
/*!
|
||||
* @brief Make a streaming GET request.
|
||||
|
@ -554,11 +561,13 @@ namespace Mastodon
|
|||
* @param ptr Pointer to the http object. Can be used to call
|
||||
* ptr->cancel_stream()
|
||||
*
|
||||
* @return @ref error "Error code".
|
||||
* @return @ref error "Errors" are reported in this format:
|
||||
* `{"error_code":uint8_t,"http_error":uint16_t}`. `http_error` is
|
||||
* optional.
|
||||
*
|
||||
* @since 0.100.0
|
||||
*/
|
||||
uint8_t get_stream(const Mastodon::API::v1 &call,
|
||||
void get_stream(const Mastodon::API::v1 &call,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream);
|
||||
|
||||
|
@ -569,11 +578,9 @@ namespace Mastodon
|
|||
* @param ptr Pointer to the http object. Can be used to call
|
||||
* ptr->cancel_stream()
|
||||
*
|
||||
* @return @ref error "Error code".
|
||||
*
|
||||
* @since 0.100.0
|
||||
*/
|
||||
uint8_t get_stream(const string &call,
|
||||
void get_stream(const string &call,
|
||||
std::unique_ptr<Mastodon::API::http> &ptr,
|
||||
string &stream);
|
||||
|
||||
|
|
Reference in New Issue