diff --git a/include/uri.hpp b/include/uri.hpp index 61e9cb2..a9ad5d2 100644 --- a/include/uri.hpp +++ b/include/uri.hpp @@ -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; /*! diff --git a/src/cli/main.cpp b/src/cli/main.cpp index e4c21d2..5e7794c 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -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; diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 9637793..2f908f0 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -28,12 +28,12 @@ #include #include #include +#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