Add try/catch

This commit is contained in:
zv0n 2022-09-24 23:13:42 +02:00
parent ae57b4c06e
commit 913c8fd168
5 changed files with 188 additions and 153 deletions

View File

@ -10,6 +10,7 @@ void LunchRest::MahostinaRestaurant::parse() {
if (html == "")
return;
clearMenus();
try {
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto today_lists = root.find(
@ -36,4 +37,10 @@ void LunchRest::MahostinaRestaurant::parse() {
std::stoi(text.substr(price_start, price_end - price_start)));
}
menus[day].setInvalidMenu(false);
} catch (std::exception &/*UNUSED*/) {
clearMenus();
for(int i = 0; i < menus.size(); i++) {
menus[i].addMeal(false, parseError, parseInfo, 0);
}
}
}

View File

@ -1,4 +1,5 @@
#include "menicka.hpp"
#include "restaurants.hpp"
#include "../network/network.hpp"
#include "../htmlparser.hpp"
#include "functions.hpp"
@ -24,6 +25,7 @@ void LunchRest::MenickaRestaurant::parse() {
if(html == "")
return;
clearMenus();
try {
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto days = root.find("//div[@class='menicka']");
@ -70,4 +72,10 @@ void LunchRest::MenickaRestaurant::parse() {
menus[cur_day].setInvalidMenu(false);
}
}
} catch (std::exception &/*UNUSED*/) {
clearMenus();
for(int i = 0; i < menus.size(); i++) {
menus[i].addMeal(false, parseError, parseInfo, 0);
}
}
}

View File

@ -2,6 +2,8 @@
#include <libxml++/libxml++.h>
namespace LunchRest {
const std::string parseError = "Could not retreive menu";
const std::string parseInfo = "Please contact the developer, likely the restaurant website changed and requires a new parser";
class UKarlaRestaurant : public Restaurant {
public:
UKarlaRestaurant() : Restaurant("https://ukarlabrno.cz/denni-menu/", "U Karla") {}

View File

@ -37,6 +37,7 @@ void LunchRest::TaoRestaurant::parse() {
if (html == "")
return;
clearMenus();
try {
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto week_meals_html =
@ -112,4 +113,10 @@ void LunchRest::TaoRestaurant::parse() {
menus[cur_day].setInvalidMenu(false);
}
}
} catch (std::exception &/*UNUSED*/) {
clearMenus();
for(int i = 0; i < menus.size(); i++) {
menus[i].addMeal(false, parseError, parseInfo, 0);
}
}
}

View File

@ -10,6 +10,7 @@ void LunchRest::UKarlaRestaurant::parse() {
if(html == "")
return;
clearMenus();
try {
HtmlParser hparse(html);
auto &root = hparse.getRoot();
auto days = root.find("//li[@class='item-day']");
@ -20,6 +21,10 @@ void LunchRest::UKarlaRestaurant::parse() {
for(auto &meal : meals) {
auto soup = false;
auto texts = meal->find("./div/text()");
if(texts.empty()) {
menus[menu_index].addMeal(false, parseError, parseInfo, 0);
continue;
}
std::string name = trim(nodeToText(texts[0]));
if(name[0] == 'P') {
soup = true;
@ -35,4 +40,10 @@ void LunchRest::UKarlaRestaurant::parse() {
}
menu_index++;
}
} catch (std::exception &/*UNUSED*/) {
clearMenus();
for(int i = 0; i < menus.size(); i++) {
menus[i].addMeal(false, parseError, parseInfo, 0);
}
}
}