Improve Errorhandling
This commit is contained in:
+86
-70
@@ -176,91 +176,107 @@ std::string HttpClientImpl::PerformOperation(const HttpRequest& request) const
|
|||||||
Logging::Log(Logging::Severity::Debug, debug.str());
|
Logging::Log(Logging::Severity::Debug, debug.str());
|
||||||
debug.str("");
|
debug.str("");
|
||||||
|
|
||||||
|
CURLcode returnValue;
|
||||||
long code = 0;
|
long code = 0;
|
||||||
curl_easy_perform(curl);
|
|
||||||
|
returnValue = curl_easy_perform(curl);
|
||||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
||||||
|
|
||||||
if (returnType != HttpRequest::ReturnType::Code)
|
if (CURLE_OK == returnValue)
|
||||||
{
|
{
|
||||||
if (code < 100)
|
if (returnType != HttpRequest::ReturnType::Code)
|
||||||
{
|
{
|
||||||
// Non-Existent
|
if (code < 100)
|
||||||
std::stringstream err;
|
|
||||||
err << "Error (" << code << ") encountered while retrieving " << url << "\n";
|
|
||||||
error = err.str();
|
|
||||||
}
|
|
||||||
else if (code < 200)
|
|
||||||
{
|
|
||||||
// Informational
|
|
||||||
std::stringstream info;
|
|
||||||
info << "Informational (" << code << ") encountered while retrieving " << url << "\n";
|
|
||||||
Logging::Log(Logging::Severity::Info, info.str());
|
|
||||||
}
|
|
||||||
else if (code < 300)
|
|
||||||
{
|
|
||||||
// Success
|
|
||||||
if (code == 202)
|
|
||||||
buffer = PerformOperation(request);
|
|
||||||
}
|
|
||||||
else if (code < 400)
|
|
||||||
{
|
|
||||||
// Redirection
|
|
||||||
if (returnType == HttpRequest::ReturnType::Redirect)
|
|
||||||
{
|
{
|
||||||
char* pRedirectUrl;
|
// Non-Existent
|
||||||
curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &pRedirectUrl);
|
std::stringstream err;
|
||||||
buffer = std::string(pRedirectUrl);
|
err << "Error (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
error = err.str();
|
||||||
|
}
|
||||||
|
else if (code < 200)
|
||||||
|
{
|
||||||
|
// Informational
|
||||||
|
std::stringstream info;
|
||||||
|
info << "Informational (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
Logging::Log(Logging::Severity::Info, info.str());
|
||||||
|
}
|
||||||
|
else if (code < 300)
|
||||||
|
{
|
||||||
|
// Success
|
||||||
|
if (code == 202)
|
||||||
|
buffer = PerformOperation(request);
|
||||||
|
}
|
||||||
|
else if (code < 400)
|
||||||
|
{
|
||||||
|
// Redirection
|
||||||
|
if (returnType == HttpRequest::ReturnType::Redirect)
|
||||||
|
{
|
||||||
|
char* pRedirectUrl;
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &pRedirectUrl);
|
||||||
|
buffer = std::string(pRedirectUrl);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::stringstream redirect;
|
||||||
|
redirect << "Redirect (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
Logging::Log(Logging::Severity::Info, redirect.str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (code < 500)
|
||||||
|
{
|
||||||
|
// Client Error
|
||||||
|
std::stringstream err;
|
||||||
|
err << "Client Error (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
err << "Response: " << buffer << "\n";
|
||||||
|
/*
|
||||||
|
if (code == 429)
|
||||||
|
{
|
||||||
|
// Too Many Requests
|
||||||
|
curl_off_t wait = 0;
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_RETRY_AFTER, &wait);
|
||||||
|
err << "Retry after " << wait << " seconds\n";
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
error = err.str();
|
||||||
|
}
|
||||||
|
else if (code < 600)
|
||||||
|
{
|
||||||
|
// Server Error
|
||||||
|
std::stringstream err;
|
||||||
|
err << "Server Error (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
err << "Response: " << buffer << "\n";
|
||||||
|
error = err.str();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::stringstream redirect;
|
// Non-Existent
|
||||||
redirect << "Redirect (" << code << ") encountered while retrieving " << url << "\n";
|
std::stringstream err;
|
||||||
Logging::Log(Logging::Severity::Info, redirect.str());
|
err << "Error (" << code << ") encountered while retrieving " << url << "\n";
|
||||||
|
err << "Response: " << buffer << "\n";
|
||||||
|
error = err.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (code < 500)
|
|
||||||
{
|
|
||||||
// Client Error
|
|
||||||
std::stringstream err;
|
|
||||||
err << "Client Error (" << code << ") encountered while retrieving " << url << "\n";
|
|
||||||
err << "Response: " << buffer << "\n";
|
|
||||||
/*
|
|
||||||
if (code == 429)
|
|
||||||
{
|
|
||||||
// Too Many Requests
|
|
||||||
curl_off_t wait = 0;
|
|
||||||
curl_easy_getinfo(curl, CURLINFO_RETRY_AFTER, &wait);
|
|
||||||
err << "Retry after " << wait << " seconds\n";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
error = err.str();
|
|
||||||
}
|
|
||||||
else if (code < 600)
|
|
||||||
{
|
|
||||||
// Server Error
|
|
||||||
std::stringstream err;
|
|
||||||
err << "Server Error (" << code << ") encountered while retrieving " << url << "\n";
|
|
||||||
err << "Response: " << buffer << "\n";
|
|
||||||
error = err.str();
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Non-Existent
|
buffer = std::to_string(code);
|
||||||
std::stringstream err;
|
|
||||||
err << "Error (" << code << ") encountered while retrieving " << url << "\n";
|
|
||||||
err << "Response: " << buffer << "\n";
|
|
||||||
error = err.str();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer = std::to_string(code);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug << "Code: " << code << std::endl;
|
debug << "Code: " << code << std::endl;
|
||||||
if (m_debugLogging)
|
if (m_debugLogging)
|
||||||
Logging::Log(Logging::Severity::Debug, debug.str());
|
Logging::Log(Logging::Severity::Debug, debug.str());
|
||||||
debug.str("");
|
debug.str("");
|
||||||
|
}
|
||||||
|
else if (CURLE_OPERATION_TIMEDOUT == returnValue)
|
||||||
|
{
|
||||||
|
std::stringstream err;
|
||||||
|
err << "Timeout (" << m_timeout << " seconds) encountered while retrieving " << url << "\n";
|
||||||
|
error = err.str();
|
||||||
|
|
||||||
|
debug << "Operation timed out" << std::endl;
|
||||||
|
if (m_debugLogging)
|
||||||
|
Logging::Log(Logging::Severity::Debug, debug.str());
|
||||||
|
debug.str("");
|
||||||
|
}
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
LIBRARIES += Logging
|
LIBRARIES += Logging
|
||||||
LIBRARIES += Utilities
|
LIBRARIES += Utilities
|
||||||
|
|
||||||
|
CFLAGS += -Wno-deprecated-declarations
|
||||||
CFLAGS += -I$(ROOTPATH)/Libraries/asio/include
|
CFLAGS += -I$(ROOTPATH)/Libraries/asio/include
|
||||||
|
|
||||||
LFLAGS += -pthread
|
LFLAGS += -pthread
|
||||||
|
|||||||
Reference in New Issue
Block a user