Add error code to zip::exception.
This commit is contained in:
parent
b96315f8bb
commit
84f600196c
|
@ -18,6 +18,7 @@
|
|||
#include "fs-compat.hpp"
|
||||
#include "options.hpp"
|
||||
#include "search.hpp"
|
||||
#include "zip.hpp"
|
||||
|
||||
#include <boost/locale/generator.hpp>
|
||||
#include <boost/locale/message.hpp>
|
||||
|
@ -145,7 +146,7 @@ int main(int argc, char *argv[])
|
|||
std::lock_guard<std::mutex> guard(mutex_matches_all);
|
||||
matches_all.emplace_back(matches);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
catch (const zip::exception &e)
|
||||
{
|
||||
cerr << translate("ERROR: ") << e.what() << '\n';
|
||||
cerr << format(translate("Error while searching {0:s}.")
|
||||
|
|
18
src/zip.cpp
18
src/zip.cpp
|
@ -26,6 +26,8 @@
|
|||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -92,6 +94,18 @@ std::string read_file(const fs::path &filepath, std::string_view entry_path)
|
|||
|
||||
struct archive *open_file(const fs::path &filepath)
|
||||
{
|
||||
std::ifstream file{filepath};
|
||||
if (!file.good())
|
||||
{
|
||||
exception e{format(translate("Could not open {0:s}: "
|
||||
"Permission denied.")
|
||||
.str(),
|
||||
filepath.string())};
|
||||
e.code = 2;
|
||||
throw exception{e};
|
||||
}
|
||||
file.close();
|
||||
|
||||
auto *zipfile{archive_read_new()};
|
||||
archive_read_support_filter_all(zipfile);
|
||||
archive_read_support_format_zip(zipfile);
|
||||
|
@ -101,8 +115,10 @@ struct archive *open_file(const fs::path &filepath)
|
|||
{
|
||||
close_file(zipfile, filepath);
|
||||
|
||||
throw exception{format(translate("Could not open {0:s}.").str(),
|
||||
exception e{format(translate("Could not open {0:s}.").str(),
|
||||
filepath.string())};
|
||||
e.code = 1;
|
||||
throw exception{e};
|
||||
}
|
||||
|
||||
return zipfile;
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include <archive.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
@ -47,6 +48,8 @@ class exception : public std::runtime_error
|
|||
{
|
||||
public:
|
||||
using std::runtime_error::runtime_error;
|
||||
|
||||
std::uint8_t code{0};
|
||||
};
|
||||
|
||||
} // namespace epubgrep::zip
|
||||
|
|
Loading…
Reference in New Issue
Block a user