Added error handling to http_extract.

This commit is contained in:
tastytea 2019-08-06 12:13:27 +02:00
parent 9bf6031b92
commit 862e90274e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 22 additions and 14 deletions

View File

@ -26,15 +26,21 @@ namespace remwharead
/*!
* @brief A processed HTML page.
*
* @since 0.6.0
* @return true if successful, when cast to bool.
*
* @since 0.7.0
*
* @headerfile uri.hpp remwharead/uri.hpp
*/
typedef struct html_extract
{
bool successful = false;
string error;
string title;
string description;
string fulltext;
operator bool();
} html_extract;
/*!

View File

@ -59,9 +59,10 @@ int main(const int argc, const char *argv[])
{
URI uri(opts.uri);
html_extract page = uri.get();
if (page.fulltext.empty())
if (!page)
{
cerr << "Error: Could not fetch page.\n";
cerr << page.error << endl;
return 4;
}
string archive_uri;

View File

@ -28,12 +28,12 @@
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <Poco/Environment.h>
#include "Poco/Exception.h"
#include "version.hpp"
#include "uri.hpp"
namespace remwharead
{
using std::uint64_t;
using std::cerr;
using std::endl;
using std::regex;
@ -53,6 +53,11 @@ namespace remwharead
using Poco::StreamCopier;
using Poco::Environment;
html_extract::operator bool()
{
return successful;
}
URI::URI(const string &uri)
:_uri(uri)
{
@ -73,6 +78,7 @@ namespace remwharead
{
proxy_env = proxy_env.substr(0, pos);
}
if ((pos = proxy_env.find(':')) != string::npos)
{
proxy.host = proxy_env.substr(0, pos);
@ -100,30 +106,25 @@ namespace remwharead
{
try
{
std::ostringstream oss;
const string answer = make_request(_uri);
if (answer.empty())
{
cerr << "Error: Could not download page.\n";
}
else
if (!answer.empty())
{
return
{
true,
"",
extract_title(answer),
extract_description(answer),
strip_html(answer)
};
}
}
catch (const std::exception &e)
catch (const Poco::Exception &e)
{
// Prints something like "SSL Exception", nothing specific.
cerr << "Error in " << __func__ << ": " << e.what() << endl;
return { false, e.displayText(), "", "", "" };
}
return { "", "", "" };
return { false, "Unknown error.", "", "", "" };
}
const string URI::make_request(const string &uri) const