From b100e4e2e8be557f5792da852a453625f5032187 Mon Sep 17 00:00:00 2001 From: Julian Arndt Date: Sun, 10 Jan 2021 21:34:27 +0100 Subject: [PATCH] Add error handling everywhere --- src/main/java/de/jeyp91/App.java | 8 +- .../apifootball/APIFootballUpdater.java | 33 +++++-- .../java/de/jeyp91/tippliga/TLWMatch.java | 2 - .../jeyp91/tippliga/TLWMatchdaysUpdater.java | 95 ++++++++++--------- .../tippliga/TLWMatchesUpdaterFootball.java | 2 +- .../de/jeyp91/tippliga/TLWTeamsUpdater.java | 2 +- 6 files changed, 79 insertions(+), 63 deletions(-) diff --git a/src/main/java/de/jeyp91/App.java b/src/main/java/de/jeyp91/App.java index 67050ae..e666efd 100644 --- a/src/main/java/de/jeyp91/App.java +++ b/src/main/java/de/jeyp91/App.java @@ -28,7 +28,7 @@ public class App { private static String configFile; private static final Logger logger = LogManager.getLogger(App.class); - public static void main(String[] args) throws Exception { + public static void main(String[] args) { ConfigurationBuilder builder = ConfigurationBuilderFactory.newConfigurationBuilder(); @@ -74,11 +74,11 @@ public class App { int forumId; if (season == 2021) { forumId = 15; + String checksum = con.getChecksumOfPost(forumId, configFile); + System.out.println(checksum); } else { - throw new Exception("Season does not exist"); + logger.error("Season does not exist"); } - String checksum = con.getChecksumOfPost(forumId, configFile); - System.out.println(checksum); break; default: break; diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java b/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java index acadeb9..4e69d82 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballUpdater.java @@ -2,10 +2,13 @@ package de.jeyp91.apifootball; import de.jeyp91.ResourceProvider; import de.jeyp91.S3Provider; +import de.jeyp91.teamidmatcher.TeamIDMatcher; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -15,36 +18,46 @@ import java.util.HashSet; public class APIFootballUpdater { + private static final Logger logger = LogManager.getLogger(APIFootballUpdater.class); + public APIFootballUpdater() { } - public void updateFixtures(int league) throws Exception { + public void updateFixtures(int league) { String apiFootballUrl = "https://v2.api-football.com/fixtures/league/" + league + "?timezone=Europe/Berlin"; - String content = getRawData(apiFootballUrl); - S3Provider prov = new S3Provider(); - prov.writeFixturesToS3(league, content); + try { + String content = getRawData(apiFootballUrl); + S3Provider prov = new S3Provider(); + prov.writeFixturesToS3(league, content); + } catch (Exception e) { + logger.error(e.getMessage()); + } } - public void updateAllFixtures() throws Exception { + public void updateAllFixtures() { HashSet leagues = getLeagues(); for (Integer league : leagues) { updateFixtures(league); } } - public void updateAllRounds() throws Exception { + public void updateAllRounds() { HashSet leagues = getLeagues(); for (Integer league : leagues) { updateRounds(league); } } - public void updateRounds(int league) throws Exception { + public void updateRounds(int league) { String apiFootballUrl = "https://v2.api-football.com/fixtures/rounds/" + league; - String content = getRawData(apiFootballUrl); - S3Provider prov = new S3Provider(); - prov.writeRoundsToS3(league, content); + try { + String content = getRawData(apiFootballUrl); + S3Provider prov = new S3Provider(); + prov.writeRoundsToS3(league, content); + } catch (Exception e) { + logger.error(e.getMessage()); + } } public String getRawData(String requestUrl) throws Exception { diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatch.java b/src/main/java/de/jeyp91/tippliga/TLWMatch.java index 042c5d5..224db53 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatch.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatch.java @@ -14,8 +14,6 @@ import org.apache.logging.log4j.Logger; */ public class TLWMatch extends BaseMatch{ - private final Logger logger = LogManager.getLogger(TLWMatch.class); - public final Integer STATUS_NOTSTARTED = 0; public final Integer STATUS_STARTED = 1; public final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2; diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java index 01db287..fb0ffc1 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java @@ -1,6 +1,8 @@ package de.jeyp91.tippliga; import de.jeyp91.tippligaforum.TippligaSQLConnector; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -9,6 +11,8 @@ import java.util.Date; public class TLWMatchdaysUpdater { + private final Logger logger = LogManager.getLogger(TLWMatch.class); + int season; int league; ArrayList matchdaysOriginal; @@ -28,66 +32,67 @@ public class TLWMatchdaysUpdater { this.matchdaysUpdated.sort(Comparator.comparing(TLWMatchday::getMatchday)); } - public String getUpdateSql() throws Exception { + public String getUpdateSql() { String updateSql = ""; if(this.matchdaysUpdated.size() != this.matchdaysOriginal.size()) { - throw new Exception("Wrong matchdays config"); - } + this.logger.error("Wrong matchdays config!"); + } else { - for(int i = 0; i < this.matchdaysUpdated.size(); i++) { - TLWMatchday matchdayOriginal = this.matchdaysOriginal.get(i); - TLWMatchday matchdayUpdated = this.matchdaysUpdated.get(i); + for (int i = 0; i < this.matchdaysUpdated.size(); i++) { + TLWMatchday matchdayOriginal = this.matchdaysOriginal.get(i); + TLWMatchday matchdayUpdated = this.matchdaysUpdated.get(i); - if(matchdayOriginal.getMatchday() != matchdayUpdated.getMatchday()) { - throw new Exception("BUUUUG!"); - } + if (matchdayOriginal.getMatchday() != matchdayUpdated.getMatchday()) { + this.logger.error("BUUUUG!"); + } else { - Date now = new Date(System.currentTimeMillis()); + Date now = new Date(System.currentTimeMillis()); - Date earliestDate = null; - try { - earliestDate = new SimpleDateFormat("yyyy-MM-dd").parse(matchdayUpdated.getDeliveryDate().substring(0, 10)); - } catch (java.text.ParseException e) { - e.printStackTrace(); - } + Date earliestDate = null; + try { + earliestDate = new SimpleDateFormat("yyyy-MM-dd").parse(matchdayUpdated.getDeliveryDate().substring(0, 10)); + } catch (java.text.ParseException e) { + e.printStackTrace(); + } - if(earliestDate.after(now)) { + if (earliestDate.after(now)) { - String updateStart = "UPDATE phpbb_footb_matchdays SET "; - String condition = "WHERE season = " + matchdayOriginal.getSeason() + " " + - "AND league = " + matchdayOriginal.getLeague() + " " + - "AND matchday = " + matchdayOriginal.getMatchday() + ";\n"; - String beautifulInfoStart = "Aktualisiere Saison " + matchdayOriginal.getSeason() + ", " + - "Liga " + matchdayOriginal.getLeague() + ", " + - "Spieltag " + matchdayOriginal.getMatchday() + ", "; + String updateStart = "UPDATE phpbb_footb_matchdays SET "; + String condition = "WHERE season = " + matchdayOriginal.getSeason() + " " + + "AND league = " + matchdayOriginal.getLeague() + " " + + "AND matchday = " + matchdayOriginal.getMatchday() + ";\n"; + String beautifulInfoStart = "Aktualisiere Saison " + matchdayOriginal.getSeason() + ", " + + "Liga " + matchdayOriginal.getLeague() + ", " + + "Spieltag " + matchdayOriginal.getMatchday() + ", "; - if (!matchdayOriginal.getDeliveryDate().equals(matchdayUpdated.getDeliveryDate())) { - updateSql += updateStart + - "delivery_date = '" + matchdayUpdated.getDeliveryDate() + "' " + - condition; - this.beautifulInfo += beautifulInfoStart + - "Tippabgabeschluss 1 zu '" + matchdayUpdated.getDeliveryDate() + "'.\n"; - } + if (!matchdayOriginal.getDeliveryDate().equals(matchdayUpdated.getDeliveryDate())) { + updateSql += updateStart + + "delivery_date = '" + matchdayUpdated.getDeliveryDate() + "' " + + condition; + this.beautifulInfo += beautifulInfoStart + + "Tippabgabeschluss 1 zu '" + matchdayUpdated.getDeliveryDate() + "'.\n"; + } - if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) { - updateSql += updateStart + - "delivery_date_2 = '" + matchdayUpdated.getDeliveryDate2() + "' " + - condition; - this.beautifulInfo += beautifulInfoStart + - "Tippabgabeschluss 2 zu '" + matchdayUpdated.getDeliveryDate2() + "'.\n"; - } + if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) { + updateSql += updateStart + + "delivery_date_2 = '" + matchdayUpdated.getDeliveryDate2() + "' " + + condition; + this.beautifulInfo += beautifulInfoStart + + "Tippabgabeschluss 2 zu '" + matchdayUpdated.getDeliveryDate2() + "'.\n"; + } - if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) { - updateSql += updateStart + - "matchday_name = '" + matchdayUpdated.getMatchdayName() + "' " + - condition; - this.beautifulInfo += beautifulInfoStart + - "Spieltagsname zu '" + matchdayUpdated.getMatchdayName() + "'.\n"; + if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) { + updateSql += updateStart + + "matchday_name = '" + matchdayUpdated.getMatchdayName() + "' " + + condition; + this.beautifulInfo += beautifulInfoStart + + "Spieltagsname zu '" + matchdayUpdated.getMatchdayName() + "'.\n"; + } + } } } } - return updateSql; } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java index 6335457..5e238f3 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java @@ -15,7 +15,7 @@ import java.time.*; import java.util.ArrayList; public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { - private static final Logger logger = LogManager.getLogger(App.class); + private static final Logger logger = LogManager.getLogger(TLWMatchesUpdaterFootball.class); ArrayList tlwMatchesOriginal; ArrayList tlwMatchesUpdated; diff --git a/src/main/java/de/jeyp91/tippliga/TLWTeamsUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWTeamsUpdater.java index 36791e2..3fb6e4c 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWTeamsUpdater.java +++ b/src/main/java/de/jeyp91/tippliga/TLWTeamsUpdater.java @@ -13,7 +13,7 @@ public class TLWTeamsUpdater { ArrayList matchesUpdated; ArrayList missingTeams; - public TLWTeamsUpdater(int season, int league, String configFileName) throws Exception { + public TLWTeamsUpdater(int season, int league, String configFileName) { TippligaSQLConnector conn = TippligaSQLConnector.getInstance();