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());
|
||||
debug.str("");
|
||||
|
||||
CURLcode returnValue;
|
||||
long code = 0;
|
||||
curl_easy_perform(curl);
|
||||
|
||||
returnValue = curl_easy_perform(curl);
|
||||
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
|
||||
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)
|
||||
if (code < 100)
|
||||
{
|
||||
char* pRedirectUrl;
|
||||
curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &pRedirectUrl);
|
||||
buffer = std::string(pRedirectUrl);
|
||||
// Non-Existent
|
||||
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;
|
||||
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
|
||||
{
|
||||
std::stringstream redirect;
|
||||
redirect << "Redirect (" << code << ") encountered while retrieving " << url << "\n";
|
||||
Logging::Log(Logging::Severity::Info, redirect.str());
|
||||
// Non-Existent
|
||||
std::stringstream err;
|
||||
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
|
||||
{
|
||||
// Non-Existent
|
||||
std::stringstream err;
|
||||
err << "Error (" << code << ") encountered while retrieving " << url << "\n";
|
||||
err << "Response: " << buffer << "\n";
|
||||
error = err.str();
|
||||
buffer = std::to_string(code);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = std::to_string(code);
|
||||
}
|
||||
|
||||
debug << "Code: " << code << std::endl;
|
||||
if (m_debugLogging)
|
||||
Logging::Log(Logging::Severity::Debug, debug.str());
|
||||
debug.str("");
|
||||
debug << "Code: " << code << std::endl;
|
||||
if (m_debugLogging)
|
||||
Logging::Log(Logging::Severity::Debug, 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user