Replaced return codes in API::http.

This commit is contained in:
tastytea 2019-02-22 12:03:28 +01:00
parent c8baf41582
commit e034dbc299
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 34 additions and 39 deletions

View File

@ -79,19 +79,16 @@ g++ -std=c++14 -lmastodon-cpp example.cpp
## Error codes
mastodon-cpp will never use error codes below 11, except 0.
| Code | Explanation |
| --------: |:---------------------------------|
| 0 | No error |
| 11 | Invalid call |
| 12 | Not implemented |
| 13 | URL changed (HTTP 301 or 308) |
| 14 | Aborted by user |
| 15 | Network error (curlpp exception) |
| 16 | Timeout |
| 100 - 999 | HTTP status codes |
| 65535 | Unknown error |
| 22 | Invalid argument |
| 78 | URL changed (HTTP 301 or 308) |
| 110 | Connection timed out |
| 111 | Connection refused |
| 132 | curlpp runtime error |
| 133 | curlpp logic error |
| 255 | Unknown error |
If you use a debug build, you get more verbose error messages.

View File

@ -44,21 +44,18 @@ API::http::~http()
curlpp::terminate();
}
uint16_t API::http::request(const method &meth,
const string &path,
string &answer)
return_call API::http::request(const method &meth, const string &path)
{
return request(meth, path, curlpp::Forms(), answer);
return request(meth, path, curlpp::Forms());
}
uint16_t API::http::request(const method &meth,
const string &path,
const curlpp::Forms &formdata,
string &answer)
return_call API::http::request(const method &meth,
const string &path,
const curlpp::Forms &formdata)
{
using namespace std::placeholders; // _1, _2, _3
uint16_t ret = 0;
string answer;
ttdebug << "Path is: " << path << '\n';
try
@ -131,31 +128,31 @@ uint16_t API::http::request(const method &meth,
answer.clear();
request.perform();
ret = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << ret << '\n';
uint16_t http_code = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << http_code << '\n';
// Work around "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK"
size_t pos = answer.find("\r\n\r\n", 25);
_headers = answer.substr(0, pos);
// Only return body
answer = answer.substr(pos + 4);
if (ret == 200 || ret == 302 || ret == 307)
if (http_code == 200 || http_code == 302 || http_code == 307)
{ // OK or Found or Temporary Redirect
return 0;
return { 0, "", http_code, answer };
}
else if (ret == 301 || ret == 308)
else if (http_code == 301 || http_code == 308)
{ // Moved Permanently or Permanent Redirect
// return new URL
answer = curlpp::infos::EffectiveUrl::get(request);
return 13;
return { 78, "Remote address changed", http_code, answer };
}
else if (ret == 0)
else if (http_code == 0)
{
return 0xffff;
return { 255, "Unknown error", http_code, answer };
}
else
{
return ret;
return { 111, "Connection refused", http_code, answer };
}
}
catch (curlpp::RuntimeError &e)
@ -166,12 +163,12 @@ uint16_t API::http::request(const method &meth,
(what.compare(0, 19, "Failed writing body") == 0))
{
ttdebug << "Request was cancelled by user\n";
return 14;
return { 0, "Request was cancelled by user", 0, "" };
}
else if (what.compare(what.size() - 20, 20, "Connection timed out") == 0)
{
ttdebug << "Timeout\n";
return 16;
return { 110, "Connection timed out", 0, "" };
}
if (parent.exceptions())
@ -181,7 +178,7 @@ uint16_t API::http::request(const method &meth,
else
{
ttdebug << "curlpp::RuntimeError: " << e.what() << std::endl;
return 15;
return { 132, e.what(), 0, "" };
}
}
catch (curlpp::LogicError &e)
@ -192,7 +189,7 @@ uint16_t API::http::request(const method &meth,
}
ttdebug << "curlpp::LogicError: " << e.what() << std::endl;
return 15;
return { 133, e.what(), 0, "" };
}
}

View File

@ -55,6 +55,9 @@ std::ostream &operator <<(std::ostream &out, const return_call &ret)
return out;
}
return_call::return_call()
{}
return_call::return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a)
: http_error_code(hec)

View File

@ -62,6 +62,7 @@ namespace Mastodon
friend std::ostream &operator <<(std::ostream &out,
const return_call &ret);
return_call();
return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a);
} return_call;
@ -116,9 +117,7 @@ public:
explicit http(const API &api, const string &instance,
const string &access_token);
~http();
uint16_t request(const method &meth,
const string &path,
string &answer);
return_call request(const method &meth, const string &path);
/*!
* @brief HTTP Request.
@ -133,10 +132,9 @@ public:
*
* @since before 0.11.0
*/
uint16_t request(const method &meth,
const string &path,
const curlpp::Forms &formdata,
string &answer);
return_call request(const method &meth,
const string &path,
const curlpp::Forms &formdata);
/*!
* @brief Get all headers in a string