Add exception for zip processing.
continuous-integration/drone/push Build is failing Details

- New dependency: libfmt.
- Translate error messages.
This commit is contained in:
tastytea 2021-05-21 03:25:42 +02:00
parent 2709be1620
commit 4e8c6e7489
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
6 changed files with 31 additions and 7 deletions

View File

@ -25,6 +25,7 @@ find_package(Boost 1.65.0 REQUIRED COMPONENTS program_options locale)
find_package(Gettext REQUIRED)
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
find_package(LibArchive REQUIRED)
find_package(fmt REQUIRED CONFIG)
add_subdirectory(src)

View File

@ -12,6 +12,7 @@
:uri-boost: https://www.boost.org/
:uri-gettext: https://www.gnu.org/software/gettext/
:uri-libarchive: https://www.libarchive.org/
:uri-fmt: https://github.com/fmtlib/fmt
*{project}* is a search tool for EPUB ebooks.
@ -41,6 +42,7 @@ image::https://repology.org/badge/vertical-allrepos/epubgrep.svg[]
* link:{uri-boost}[Boost] (tested: 1.75.0 / 1.65.0)
* link:{uri-gettext}[gettext] (tested: 0.21 / 0.19)
* link:{uri-libarchive}[libarchive] (tested: 3.5 / 3.2)
* link:{uri-fmt}[fmt] (tested: 7.0 / 4.0)
* Optional
** Tests: link:{uri-catch}[Catch] (tested: 2.13 / 1.10)

View File

@ -14,7 +14,7 @@ unset(headers_src)
target_link_libraries(${PROJECT_NAME}
Boost::program_options Boost::locale
std::filesystem LibArchive::LibArchive)
std::filesystem LibArchive::LibArchive fmt::fmt)
target_include_directories(${PROJECT_NAME}
PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")

View File

@ -71,9 +71,17 @@ int main(int argc, char *argv[])
for (const auto &file : vm["input-file"].as<std::vector<std::string>>())
{
cout << " " << file << ":\n";
for (const auto &entry : epubgrep::zip::list(file))
try
{
cout << " " << entry << '\n';
for (const auto &entry : epubgrep::zip::list(file))
{
cout << " " << entry << '\n';
}
}
catch (const epubgrep::zip::exception &e)
{
cerr << translate("ERROR: ") << e.what() << '\n';
cerr << translate("Error while processing epub file.") << '\n';
}
}
}

View File

@ -20,6 +20,9 @@
#include <archive.h>
#include <archive_entry.h>
#include <boost/locale/message.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h> // For compatibility with fmt 4.
#include <string>
#include <vector>
@ -27,6 +30,9 @@
namespace epubgrep::zip
{
using boost::locale::translate;
using fmt::format;
std::vector<std::string> list(const fs::path &filepath)
{
struct archive *zipfile{};
@ -41,8 +47,8 @@ std::vector<std::string> list(const fs::path &filepath)
result = archive_read_open_filename(zipfile, filepath.c_str(), 10240);
if (result != ARCHIVE_OK)
{
// NOLINTNEXTLINE
throw "Zip file not okay. 🙁";
throw exception{format(translate("Could not open {0:s}.").str(),
filepath.string())};
}
while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK)
@ -54,8 +60,8 @@ std::vector<std::string> list(const fs::path &filepath)
result = archive_read_free(zipfile);
if (result != ARCHIVE_OK)
{
// NOLINTNEXTLINE
throw "Error closing zipfile. 🙁";
throw exception{format(translate("ERROR: Could not close {0:s}.").str(),
filepath.string())};
}
return toc;

View File

@ -19,6 +19,7 @@
#include "fs-compat.hpp"
#include <stdexcept>
#include <string>
#include <vector>
@ -27,6 +28,12 @@ namespace epubgrep::zip
std::vector<std::string> list(const fs::path &filepath);
class exception : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
} // namespace epubgrep::zip
#endif // EPUBGREP_ZIP_HPP