mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-21 14:40:41 +02:00
curl_exception: add error() function and improve message handling
This commit is contained in:
parent
2282c2875c
commit
481a2a8357
3 changed files with 13 additions and 8 deletions
|
|
@ -19,12 +19,16 @@
|
|||
#include "curl_exception.h"
|
||||
using namespace bot::exception;
|
||||
|
||||
curl_exception::curl_exception(const std::string &message, CURLcode error_code) : m_message(message), m_error_code(error_code) {
|
||||
curl_exception::curl_exception(const std::string &message, CURLcode error) : m_message(message), m_error(error) {
|
||||
}
|
||||
|
||||
curl_exception::~curl_exception() noexcept {
|
||||
}
|
||||
|
||||
const char* curl_exception::what() const noexcept {
|
||||
return m_message.c_str();
|
||||
CURLcode curl_exception::error() const noexcept {
|
||||
return m_error;
|
||||
}
|
||||
|
||||
const char* curl_exception::what() const noexcept {
|
||||
return !m_message.empty() ? m_message.c_str() : curl_easy_strerror(m_error);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,13 +27,14 @@ namespace bot {
|
|||
namespace exception {
|
||||
class curl_exception : public std::exception {
|
||||
public:
|
||||
explicit curl_exception(const std::string &message, CURLcode error_code);
|
||||
explicit curl_exception(const std::string &message, CURLcode error);
|
||||
virtual ~curl_exception() noexcept;
|
||||
CURLcode error() const noexcept;
|
||||
const char* what() const noexcept override;
|
||||
|
||||
private:
|
||||
const std::string m_message;
|
||||
const CURLcode m_error_code;
|
||||
std::string m_message;
|
||||
CURLcode m_error;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ const http_response http_request::get(const std::string &url, const http_headers
|
|||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||
curl_easy_reset(instance);
|
||||
return result == CURLE_OK ?
|
||||
response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result);
|
||||
response : throw bot::exception::curl_exception(error, result);
|
||||
}
|
||||
|
||||
const http_response http_request::post(const std::string &url, const std::string &content, const http_headers &headers) {
|
||||
|
|
@ -58,7 +58,7 @@ const http_response http_request::post(const std::string &url, const std::string
|
|||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||
curl_easy_reset(instance);
|
||||
return result == CURLE_OK ?
|
||||
response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result);
|
||||
response : throw bot::exception::curl_exception(error, result);
|
||||
}
|
||||
|
||||
std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue