From 7267981c373c68d5670beed011ab06935fedbee0 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 31 Dec 2019 13:29:45 +0100 Subject: [PATCH] Add ParseException. --- man/mastorss.1.adoc | 1 + src/document.cpp | 2 ++ src/exceptions.cpp | 9 +++++++++ src/exceptions.hpp | 12 ++++++++++++ src/main.cpp | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/man/mastorss.1.adoc b/man/mastorss.1.adoc index af4e00d..7b0474b 100644 --- a/man/mastorss.1.adoc +++ b/man/mastorss.1.adoc @@ -165,6 +165,7 @@ SOCKS proxy support yet, sorry. | 3 | File error. | 4 | Mastodon API error. | 5 | JSON error, most likely the file is wrongly formatted. +| 6 | Feed parse error. Usually the type of feed could not be detected. | 9 | Unknown error. |=============================================================================== diff --git a/src/document.cpp b/src/document.cpp index 708ff8a..6988ca2 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -141,6 +141,8 @@ void Document::parse() BOOST_LOG_TRIVIAL(debug) << "RSS detected."; parse_rss(tree); } + + ParseException{"Could not detect type of feed."}; } void Document::parse_rss(const pt::ptree &tree) diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 7cf25aa..93bb290 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -63,4 +63,13 @@ const char *FileException::what() const noexcept { return _message.c_str(); } + +ParseException::ParseException(string message) + : _message{move(message)} +{} + +const char *ParseException::what() const noexcept +{ + return _message.c_str(); +} } // namespace mastorss diff --git a/src/exceptions.hpp b/src/exceptions.hpp index 555e7a4..f672790 100644 --- a/src/exceptions.hpp +++ b/src/exceptions.hpp @@ -68,6 +68,18 @@ public: [[nodiscard]] const char *what() const noexcept override; +private: + const string _message; +}; + +class ParseException : public exception +{ +public: + explicit ParseException(string message); + + [[nodiscard]] + const char *what() const noexcept override; + private: const string _message; }; diff --git a/src/main.cpp b/src/main.cpp index c81f5ad..af09286 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ constexpr int network = 2; constexpr int file = 3; constexpr int mastodon = 4; constexpr int json = 5; +constexpr int parse = 6; constexpr int unknown = 9; } // namespace error @@ -142,6 +143,11 @@ int main(int argc, char *argv[]) cerr << "JSON error:\n" << e.what() << '\n'; return error::json; } + catch (const ParseException &e) + { + cerr << e.what() << '\n'; + return error::parse; + } catch (const runtime_error &e) { cerr << e.what() << '\n';