Added error handling to http_extract.
This commit is contained in:
parent
9bf6031b92
commit
862e90274e
|
@ -26,15 +26,21 @@ namespace remwharead
|
||||||
/*!
|
/*!
|
||||||
* @brief A processed HTML page.
|
* @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
|
* @headerfile uri.hpp remwharead/uri.hpp
|
||||||
*/
|
*/
|
||||||
typedef struct html_extract
|
typedef struct html_extract
|
||||||
{
|
{
|
||||||
|
bool successful = false;
|
||||||
|
string error;
|
||||||
string title;
|
string title;
|
||||||
string description;
|
string description;
|
||||||
string fulltext;
|
string fulltext;
|
||||||
|
|
||||||
|
operator bool();
|
||||||
} html_extract;
|
} html_extract;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
@ -59,9 +59,10 @@ int main(const int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
URI uri(opts.uri);
|
URI uri(opts.uri);
|
||||||
html_extract page = uri.get();
|
html_extract page = uri.get();
|
||||||
if (page.fulltext.empty())
|
if (!page)
|
||||||
{
|
{
|
||||||
cerr << "Error: Could not fetch page.\n";
|
cerr << "Error: Could not fetch page.\n";
|
||||||
|
cerr << page.error << endl;
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
string archive_uri;
|
string archive_uri;
|
||||||
|
|
|
@ -28,12 +28,12 @@
|
||||||
#include <Poco/StreamCopier.h>
|
#include <Poco/StreamCopier.h>
|
||||||
#include <Poco/URI.h>
|
#include <Poco/URI.h>
|
||||||
#include <Poco/Environment.h>
|
#include <Poco/Environment.h>
|
||||||
|
#include "Poco/Exception.h"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "uri.hpp"
|
#include "uri.hpp"
|
||||||
|
|
||||||
namespace remwharead
|
namespace remwharead
|
||||||
{
|
{
|
||||||
using std::uint64_t;
|
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using std::regex;
|
using std::regex;
|
||||||
|
@ -53,6 +53,11 @@ namespace remwharead
|
||||||
using Poco::StreamCopier;
|
using Poco::StreamCopier;
|
||||||
using Poco::Environment;
|
using Poco::Environment;
|
||||||
|
|
||||||
|
html_extract::operator bool()
|
||||||
|
{
|
||||||
|
return successful;
|
||||||
|
}
|
||||||
|
|
||||||
URI::URI(const string &uri)
|
URI::URI(const string &uri)
|
||||||
:_uri(uri)
|
:_uri(uri)
|
||||||
{
|
{
|
||||||
|
@ -73,6 +78,7 @@ namespace remwharead
|
||||||
{
|
{
|
||||||
proxy_env = proxy_env.substr(0, pos);
|
proxy_env = proxy_env.substr(0, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((pos = proxy_env.find(':')) != string::npos)
|
if ((pos = proxy_env.find(':')) != string::npos)
|
||||||
{
|
{
|
||||||
proxy.host = proxy_env.substr(0, pos);
|
proxy.host = proxy_env.substr(0, pos);
|
||||||
|
@ -100,30 +106,25 @@ namespace remwharead
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
|
||||||
|
|
||||||
const string answer = make_request(_uri);
|
const string answer = make_request(_uri);
|
||||||
if (answer.empty())
|
if (!answer.empty())
|
||||||
{
|
|
||||||
cerr << "Error: Could not download page.\n";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
{
|
{
|
||||||
|
true,
|
||||||
|
"",
|
||||||
extract_title(answer),
|
extract_title(answer),
|
||||||
extract_description(answer),
|
extract_description(answer),
|
||||||
strip_html(answer)
|
strip_html(answer)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const std::exception &e)
|
catch (const Poco::Exception &e)
|
||||||
{
|
{
|
||||||
// Prints something like "SSL Exception", nothing specific.
|
return { false, e.displayText(), "", "", "" };
|
||||||
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { "", "", "" };
|
return { false, "Unknown error.", "", "", "" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const string URI::make_request(const string &uri) const
|
const string URI::make_request(const string &uri) const
|
||||||
|
|
Loading…
Reference in New Issue