Properly return errors to the user
https://awoo.space/@noiob gives "This method requires an authenticated user"
This commit is contained in:
parent
dc3758004b
commit
fa68fda5ac
38
client.cpp
38
client.cpp
|
@ -70,12 +70,11 @@ std::optional<Account> MastodonClient::get_account_by_username(const std::string
|
|||
using namespace std::string_literals;
|
||||
|
||||
try {
|
||||
std::string resp = this->_send_request("https://"s + host + "/api/v1/accounts/lookup?acct=" + username);
|
||||
Account res = nlohmann::json::parse(std::move(resp));
|
||||
res.same_server = host == res.server;
|
||||
return res;
|
||||
} catch (const CurlException& e) {
|
||||
if (e.code != CURLE_HTTP_RETURNED_ERROR || this->_response_status_code() != 404) {
|
||||
Account account = this->_send_request("https://"s + host + "/api/v1/accounts/lookup?acct=" + username);
|
||||
account.same_server = host == account.server;
|
||||
return account;
|
||||
} catch (const MastodonException& e) {
|
||||
if (e.response_code != 404) {
|
||||
throw;
|
||||
}
|
||||
|
||||
|
@ -86,8 +85,7 @@ std::optional<Account> MastodonClient::get_account_by_username(const std::string
|
|||
std::vector<Post> MastodonClient::get_pinned_posts(const std::string& host, const std::string& account_id) {
|
||||
using namespace std::string_literals;
|
||||
|
||||
std::string resp = this->_send_request("https://"s + host + "/api/v1/accounts/" + account_id + "/statuses?pinned=true");
|
||||
std::vector<Post> posts = nlohmann::json::parse(std::move(resp));
|
||||
std::vector<Post> posts = this->_send_request("https://"s + host + "/api/v1/accounts/" + account_id + "/statuses?pinned=true");
|
||||
|
||||
for (Post& post : posts) {
|
||||
handle_post_server(post, host);
|
||||
|
@ -114,8 +112,7 @@ std::vector<Post> MastodonClient::get_posts(const std::string& host, const std::
|
|||
url += '?';
|
||||
url += query;
|
||||
}
|
||||
std::string resp = this->_send_request(url);
|
||||
std::vector<Post> posts = nlohmann::json::parse(std::move(resp));
|
||||
std::vector<Post> posts = this->_send_request(url);
|
||||
|
||||
for (Post& post : posts) {
|
||||
handle_post_server(post, host);
|
||||
|
@ -128,12 +125,11 @@ std::optional<Post> MastodonClient::get_post(const std::string& host, const std:
|
|||
using namespace std::string_literals;
|
||||
|
||||
try {
|
||||
std::string resp = this->_send_request("https://"s + host + "/api/v1/statuses/" + id);
|
||||
Post post = nlohmann::json::parse(std::move(resp));
|
||||
Post post = this->_send_request("https://"s + host + "/api/v1/statuses/" + id);
|
||||
handle_post_server(post, host);
|
||||
return post;
|
||||
} catch (const CurlException& e) {
|
||||
if (e.code != CURLE_HTTP_RETURNED_ERROR || this->_response_status_code() != 404) {
|
||||
} catch (const MastodonException& e) {
|
||||
if (e.response_code != 404) {
|
||||
throw;
|
||||
}
|
||||
|
||||
|
@ -144,8 +140,7 @@ std::optional<Post> MastodonClient::get_post(const std::string& host, const std:
|
|||
PostContext MastodonClient::get_post_context(const std::string& host, const std::string& id) {
|
||||
using namespace std::string_literals;
|
||||
|
||||
std::string resp = this->_send_request("https://"s + host + "/api/v1/statuses/" + id + "/context");
|
||||
PostContext context = nlohmann::json::parse(std::move(resp));
|
||||
PostContext context = this->_send_request("https://"s + host + "/api/v1/statuses/" + id + "/context");
|
||||
|
||||
for (Post& post : context.ancestors) {
|
||||
handle_post_server(post, host);
|
||||
|
@ -166,7 +161,6 @@ CURL* MastodonClient::_get_easy() {
|
|||
}
|
||||
|
||||
try {
|
||||
setopt(curl, CURLOPT_FAILONERROR, 1L);
|
||||
setopt(curl, CURLOPT_TIMEOUT_MS, 10000L);
|
||||
setopt(curl, CURLOPT_PROTOCOLS_STR, "https");
|
||||
setopt(curl, CURLOPT_USERAGENT, "Coyote (https://gitlab.com/blankX/coyote; blankie@nixnetmail.com)");
|
||||
|
@ -186,7 +180,7 @@ CURL* MastodonClient::_get_easy() {
|
|||
return curl;
|
||||
}
|
||||
|
||||
std::string MastodonClient::_send_request(const std::string& url) {
|
||||
nlohmann::json MastodonClient::_send_request(const std::string& url) {
|
||||
std::string res;
|
||||
CURL* curl = this->_get_easy();
|
||||
|
||||
|
@ -198,7 +192,13 @@ std::string MastodonClient::_send_request(const std::string& url) {
|
|||
throw CurlException(code);
|
||||
}
|
||||
|
||||
return res;
|
||||
long response_code = this->_response_status_code();
|
||||
nlohmann::json j = nlohmann::json::parse(std::move(res));
|
||||
if (response_code != 200) {
|
||||
throw MastodonException(response_code, j.at("error").get<std::string>());
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
long MastodonClient::_response_status_code() {
|
||||
|
|
17
client.h
17
client.h
|
@ -5,6 +5,7 @@
|
|||
#include <exception>
|
||||
#include <pthread.h>
|
||||
#include <curl/curl.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "models.h"
|
||||
|
||||
|
@ -30,6 +31,20 @@ public:
|
|||
CURLSHcode code;
|
||||
};
|
||||
|
||||
class MastodonException : public std::exception {
|
||||
public:
|
||||
MastodonException(long response_code_, std::string error) : response_code(response_code_), _error(std::move(error)) {}
|
||||
|
||||
const char* what() const noexcept {
|
||||
return this->_error.c_str();
|
||||
}
|
||||
|
||||
long response_code;
|
||||
|
||||
private:
|
||||
std::string _error;
|
||||
};
|
||||
|
||||
class MastodonClient {
|
||||
public:
|
||||
MastodonClient(const MastodonClient&&) = delete;
|
||||
|
@ -58,7 +73,7 @@ public:
|
|||
|
||||
private:
|
||||
CURL* _get_easy();
|
||||
std::string _send_request(const std::string& url);
|
||||
nlohmann::json _send_request(const std::string& url);
|
||||
long _response_status_code();
|
||||
|
||||
std::mutex _share_locks[CURL_LOCK_DATA_LAST];
|
||||
|
|
Loading…
Reference in New Issue