From 51a10b3d8fc3a0543938ad9234dc91b53c709a75 Mon Sep 17 00:00:00 2001 From: Michael Fross Date: Fri, 1 Dec 2023 15:47:25 -0600 Subject: [PATCH] Added more error handling to holiday function --- pom.xml | 2 +- snap/snapcraft.yaml | 2 +- src/main/java/org/fross/cal/Holidays.java | 37 +++++++++++++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index b61668c..c8c0b8e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.fross cal - 2.5.1 + 2.5.2 jar cal diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index c2bf26a..98aa722 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: fcal -version: '2.5.1' +version: '2.5.2' summary: Command line calendar display description: | fCal is a command line calendar utility. It will display a diff --git a/src/main/java/org/fross/cal/Holidays.java b/src/main/java/org/fross/cal/Holidays.java index cd6b838..8c8061c 100644 --- a/src/main/java/org/fross/cal/Holidays.java +++ b/src/main/java/org/fross/cal/Holidays.java @@ -57,16 +57,24 @@ public static TreeMap getHolidays(int year) { // Build the ISO3 to ISO2 Country Map buildCountryCodeMap(); - // Debug - Output.debugPrintln("Locale Information: "); - Output.debugPrintln(" - ISO3 Code: " + locale.getISO3Country()); - Output.debugPrintln(" - ISO2 Code: " + countryMap.get(locale.getISO3Country())); - Output.debugPrintln(" - Name: " + locale.getDisplayCountry()); - - // Build the URL and fetch the data - // https://date.nager.at/api/v3/publicholidays/2023/US - // TESTING: URL = URL + year + "/" + "MX"; - URL = URL + year + "/" + countryMap.get(locale.getISO3Country()); + try { + // Debug + Output.debugPrintln("Locale Information: "); + Output.debugPrintln(" - ISO3 Code: " + locale.getISO3Country()); + Output.debugPrintln(" - ISO2 Code: " + countryMap.get(locale.getISO3Country())); + Output.debugPrintln(" - Name: " + locale.getDisplayCountry()); + + // Build the URL and fetch the data + // https://date.nager.at/api/v3/publicholidays/2023/US + URL = URL + year + "/" + countryMap.get(locale.getISO3Country()); + + } catch (Exception Ex) { + // Couldn't retrieve the holidays - turn off holiday display + Holidays.setDisplayHolidays(false); + Output.printColorln(Ansi.Color.RED, "It doesn't look like the following country is supported: '" + locale.getDisplayCountry() + "'\n"); + return null; + } + Output.debugPrintln("URL to use: " + URL); // Pull the JSON holidays from the website @@ -77,7 +85,7 @@ public static TreeMap getHolidays(int year) { } catch (Exception ex) { // Couldn't retrieve the holidays - turn off holiday display Holidays.setDisplayHolidays(false); - Output.printColorln(Ansi.Color.RED, "Unable to retrieve holiday information from the internet\n"); + Output.printColorln(Ansi.Color.RED, "Unable to retrieve holidays for " + locale.getDisplayCountry() + "\n"); return null; } @@ -94,9 +102,12 @@ public static TreeMap getHolidays(int year) { for (int holidayEntry = 0; holidayEntry < gsonMap.length; holidayEntry++) { holidays.put(gsonMap[holidayEntry].get("date").toString(), gsonMap[holidayEntry].get("localName").toString()); } + } catch (Exception ex) { - System.out.println(ex.getMessage()); - System.out.println(ex.getStackTrace()); + // Couldn't retrieve the holidays - turn off holiday display + Holidays.setDisplayHolidays(false); + Output.printColorln(Ansi.Color.RED, "Unable to process holidays for " + locale.getDisplayCountry() + "\n"); + return null; } return holidays;