From 552df1a49eefc8e67f417b63581a26cc82f165e7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 20 Aug 2021 17:51:44 +0200 Subject: [PATCH] Don't crash if language detection fails. If there is no container.xml or something unexpected happens, we just return an empty string. --- src/book.cpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/book.cpp b/src/book.cpp index 5d1ff5e..bd1eba6 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -58,23 +58,34 @@ book read(const fs::path filepath, const bool raw) book current_book; current_book.language = [&filepath]() -> string { - pugi::xml_document xml; - auto opf_file_path{get_opf_file_path(filepath)}; - const std::string opf_file{ - zip::read_file(filepath, opf_file_path.string())}; - - const auto result{xml.load_buffer(&opf_file[0], opf_file.size())}; - if (result) + try { - auto lang{ - xml.child("package").child("metadata").child("dc:language")}; - if (lang == nullptr) + pugi::xml_document xml; + auto opf_file_path{get_opf_file_path(filepath)}; + const std::string opf_file{ + zip::read_file(filepath, opf_file_path.string())}; + + const auto result{xml.load_buffer(&opf_file[0], opf_file.size())}; + if (result) { - lang = xml.child("opf:package") - .child("opf:metadata") - .child("dc:language"); + auto lang{xml.child("package") + .child("metadata") + .child("dc:language")}; + if (lang == nullptr) + { + lang = xml.child("opf:package") + .child("opf:metadata") + .child("dc:language"); + } + return lang.text().as_string(); + } + } + catch (epubgrep::zip::exception &e) + { + if (e.code != 1) // 1 == container.xml not found. + { + LOG(log::sev::error) << e.what(); } - return lang.text().as_string(); } return ""; }();